CountDownLatch 和 CyclicBarrier
CountDownLatch 和 CyclicBarrier 适用场景都是某一个任务想要往下执行必须依靠其他任务的执行完毕才可以。
举个例子:工人干活,工人完工之后,老板才能检查。
CountDownLatch
CountDownLatch 是设定一个计数器,当其它任务通过 countDown() 方法将计数器值减为 0 时触发阻塞在 await() 方法的任务。
Worker:
public class Worker implements Runnable {
private CountDownLatch countDownLatch;
private String name;
public Worker(String name, CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
this.name = name;
}
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 + "正在干活...");
}
}
Boss:
public class Boss implements Runnable {
private CountDownLatch countDownLatch;
public Boss(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
public void run() {
System.out.println("老板等待所有人干完活...");
try {
System.out.println("我的心在等待。。。在呀么在等待额");
//计数器不为 0 时,阻塞住
countDownLatch.await();
System.out.println("等待完毕!!");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("所有人干完了...老板开始检查了...");
}
}
demo:
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
CyclicBarrier 和 CountDownLatch 功能一样,不过它能循环使用。
worker:
public class Worker extends Thread{
private String name;
private CyclicBarrier cyclicBarrier;
public Worker(String name, CyclicBarrier cyclicBarrier) {
this.name = name;
this.cyclicBarrier = cyclicBarrier;
}
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();
}
}
}
Boss:
public class Boss implements Runnable{
public void run() {
System.out.println("boss 开始检查了...");
}
}
demo:
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();
}
}
我们再创建三个 worker 测试一下:
public class CyclicBarrierDemo {
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();
}
}
从上图可以看出触发了 Boss 任务两次。