CountDownLatch 和 CyclicBarrier
适用场景都是某一个任务想要往下执行必须依靠其他任务的执行完毕才可以。
CountDownLatch 代码示例:
先看结构:
Boss:
package com.zy.thread_current.count_down_latch;
import java.util.concurrent.CountDownLatch;
/**
* @Author: zy
* @Date: 2024-11-28-22:56
* @Description:
* CountDownLatch 和 CyclicBarrier
* 适用场景都是某一个任务想要往下执行必须依靠其他任务的执行完毕才可以。
*/
public class Boss implements Runnable {
private CountDownLatch countDownLatch;
public Boss(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
System.out.println("老板等待所有人干完活...");
try {
System.out.println("我的心在等待。。。在呀么在等待额");
//计数器不为 0 时,阻塞住
countDownLatch.await();
System.out.println("等待完毕!!");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("所有人干完了...老板开始检查了...");
}
}
Worker:
package com.zy.thread_current.count_down_latch;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* @Author: zy
* @Date: 2024-11-28-22:40
* @Description:
* CountDownLatch 和 CyclicBarrier
* 适用场景都是某一个任务想要往下执行必须依靠其他任务的执行完毕才可以。
*/
public class Worker implements Runnable {
private CountDownLatch countDownLatch;
private String name;
public Worker(String name, CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
this.name = name;
}
@Override
public void run() {
this.doWork();
try {
//sleep 5s
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + "活干完了...");
//计数器减一
countDownLatch.countDown();
}
public void doWork() {
System.out.println(name + "正在干活...");
}
}
CountDownLatchDemo:
package com.zy.thread_current.count_down_latch;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Author: zy
* @Date: 2024-11-28-22:57
* @Description:
*/
public class CountDownLatchDemo {
public static void main(String[] args) {
//创建一个线程池
ExecutorService executorService = Executors.newCachedThreadPool();
//共用一个 CountDownLatch,设定计数值为 3
CountDownLatch countDownLatch = new CountDownLatch(3);
Worker worker1 = new Worker("张三", countDownLatch);
Worker worker2 = new Worker("李四", countDownLatch);
Worker worker3 = new Worker("王五", countDownLatch);
Boss boss = new Boss(countDownLatch);
//执行任务,boss任务会阻塞在 await() 直到 countDownLatch 值为 0
executorService.execute(worker1);
executorService.execute(worker2);
executorService.execute(worker3);
executorService.execute(boss);
executorService.shutdown();
}
}
CyclicBarrier代码示例:
Boss:
package com.zy.thread_current.cyclic_barrier;
/**
* @Author: zy
* @Date: 2024-11-28-23:01
* @Description:
*/
public class Boss implements Runnable {
@Override
public void run() {
System.out.println("boss 开始检查了...");
}
}
Worker:
package com.zy.thread_current.cyclic_barrier;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
/**
* @Author: zy
* @Date: 2024-11-28-23:00
* @Description:
* CyclicBarrier 和 CountDownLatch 功能一样,不过它能循环使用。
*/
public class Worker extends Thread {
private String name;
private CyclicBarrier cyclicBarrier;
public Worker(String name, CyclicBarrier cyclicBarrier) {
this.name = name;
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
System.out.println(name + "开始干活...");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + "干活完毕...");
try {
//当所有 Worker 执行到 await() 才会触发 Boss 任务
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
CyclicBarrierDemo:
package com.zy.thread_current.cyclic_barrier;
import java.util.concurrent.CyclicBarrier;
/**
* @Author: zy
* @Date: 2024-11-28-23:01
* @Description:
* CountDownLatch 和 CyclicBarrier 适用场景都是某一个任务想要往下执行必须依靠其他任务的执行完毕才可以。
*/
public class CyclicBarrierDemo {
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Boss());
new Worker("张三", cyclicBarrier).start();
new Worker("李四", cyclicBarrier).start();
new Worker("王五", cyclicBarrier).start();
}
// public static void main(String[] args) {
// //每三次触发一次 Boss 任务
// CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Boss());
//
// new Worker("张三", cyclicBarrier).start();
// new Worker("李四", cyclicBarrier).start();
// new Worker("王五", cyclicBarrier).start();
//
// new Worker("小红", cyclicBarrier).start();
// new Worker("小明", cyclicBarrier).start();
// new Worker("小白", cyclicBarrier).start();
// }
}