转自http://blog.csdn.net/zhutulang/article/details/48504487
首先,我们来看一个应用场景1:
假设一条流水线上有三个工作者:worker0,worker1,worker2。有一个任务的完成需要他们三者协作完成,worker2可以开始这个任务的前提是worker0和worker1完成了他们的工作,而worker0和worker1是可以并行他们各自的工作的。
如果我们要编码模拟上面的场景的话,我们大概很容易就会想到可以用join来做。当在当前线程中调用某个线程 thread 的 join() 方法时,当前线程就会阻塞,直到thread 执行完成,当前线程才可以继续往下执行。补充下:join的工作原理是,不停检查thread是否存活,如果存活则让当前线程永远wait,直到thread线程终止,线程的this.notifyAll 就会被调用。
我们首先用join来模拟这个场景:
Worker类如下:
- package com.concurrent.test3;
-
-
-
-
-
-
- public class Worker extends Thread {
-
-
- private String name;
-
- private long time;
-
- public Worker(String name, long time) {
- this.name = name;
- this.time = time;
- }
-
- @Override
- public void run() {
-
- try {
- System.out.println(name+"开始工作");
- Thread.sleep(time);
- System.out.println(name+"工作完成,耗费时间="+time);
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- }
- }
Test类如下:
- package com.concurrent.test3;
-
-
- public class Test {
-
- public static void main(String[] args) throws InterruptedException {
-
-
- Worker worker0 = new Worker("worker0", (long) (Math.random()*2000+3000));
- Worker worker1 = new Worker("worker1", (long) (Math.random()*2000+3000));
- Worker worker2 = new Worker("worker2", (long) (Math.random()*2000+3000));
-
- worker0.start();
- worker1.start();
-
- worker0.join();
- worker1.join();
- System.out.println("准备工作就绪");
-
- worker2.start();
- }
- }
运行test,观察控制台输出的顺序,我们发现这样可以满足需求,worker2确实是等worker0和worker1完成之后才开始工作的:
worker1开始工作
worker0开始工作
worker1工作完成,耗费时间=3947
worker0工作完成,耗费时间=4738
准备工作就绪
worker2开始工作
worker2工作完成,耗费时间=4513
除了用join外,用CountDownLatch 也可以完成这个需求。需要对worker做一点修改,我把它放在另一个包下:
Worker:
- package com.concurrent.test4;
-
- import java.util.concurrent.CountDownLatch;
-
-
-
-
-
-
- public class Worker extends Thread {
-
-
- private String name;
-
- private long time;
-
- private CountDownLatch countDownLatch;
-
- public Worker(String name, long time, CountDownLatch countDownLatch) {
- this.name = name;
- this.time = time;
- this.countDownLatch = countDownLatch;
- }
-
- @Override
- public void run() {
-
- try {
- System.out.println(name+"开始工作");
- Thread.sleep(time);
- System.out.println(name+"工作完成,耗费时间="+time);
- countDownLatch.countDown();
- System.out.println("countDownLatch.getCount()="+countDownLatch.getCount());
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- }
- }
Test:
- package com.concurrent.test4;
-
- import java.util.concurrent.CountDownLatch;
-
-
- public class Test {
-
- public static void main(String[] args) throws InterruptedException {
-
-
- CountDownLatch countDownLatch = new CountDownLatch(2);
- Worker worker0 = new Worker("worker0", (long) (Math.random()*2000+3000), countDownLatch);
- Worker worker1 = new Worker("worker1", (long) (Math.random()*2000+3000), countDownLatch);
- Worker worker2 = new Worker("worker2", (long) (Math.random()*2000+3000), countDownLatch);
-
- worker0.start();
- worker1.start();
-
- countDownLatch.await();
- System.out.println("准备工作就绪");
- worker2.start();
- }
- }
我们创建了一个计数器为2的 CountDownLatch ,让Worker持有这个CountDownLatch 实例,当完成自己的工作后,调用countDownLatch.
countDown
() 方法将计数器减1。countDownLatch.await() 方法会一直阻塞直到计数器为0,主线程才会继续往下执行。观察运行结果,发现这样也是可以的:
worker1开始工作
worker0开始工作
worker0工作完成,耗费时间=3174
countDownLatch.getCount()=1
worker1工作完成,耗费时间=3870
countDownLatch.getCount()=0
准备工作就绪
worker2开始工作
worker2工作完成,耗费时间=3992
countDownLatch.getCount()=0
那么既然如此,CountDownLatch与join的区别在哪里呢?事实上在这里我们只要考虑另一种场景,就可以很清楚地看到它们的不同了。
应用场景2:
假设worker的工作可以分为两个阶段,work2 只需要等待work0和work1完成他们各自工作的第一个阶段之后就可以开始自己的工作了,而不是场景1中的必须等待work0和work1把他们的工作全部完成之后才能开始。
试想下,在这种情况下,join是没办法实现这个场景的,而CountDownLatch却可以,因为它持有一个计数器,只要计数器为0,那么主线程就可以结束阻塞往下执行。我们可以在worker0和worker1完成第一阶段工作之后就把计数器减1即可,这样worker0和worker1在完成第一阶段工作之后,worker2就可以开始工作了。
worker:
- package com.concurrent.test5;
-
- import java.util.concurrent.CountDownLatch;
-
-
-
-
-
-
- public class Worker extends Thread {
-
-
- private String name;
-
- private long time;
-
- private CountDownLatch countDownLatch;
-
- public Worker(String name, long time, CountDownLatch countDownLatch) {
- this.name = name;
- this.time = time;
- this.countDownLatch = countDownLatch;
- }
-
- @Override
- public void run() {
-
- try {
- System.out.println(name+"开始工作");
- Thread.sleep(time);
- System.out.println(name+"第一阶段工作完成");
-
- countDownLatch.countDown();
-
- Thread.sleep(2000);
- System.out.println(name+"第二阶段工作完成");
- System.out.println(name+"工作完成,耗费时间="+(time+2000));
-
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- }
- }
Test:
- package com.concurrent.test5;
-
- import java.util.concurrent.CountDownLatch;
-
-
- public class Test {
-
- public static void main(String[] args) throws InterruptedException {
-
-
- CountDownLatch countDownLatch = new CountDownLatch(2);
- Worker worker0 = new Worker("worker0", (long) (Math.random()*2000+3000), countDownLatch);
- Worker worker1 = new Worker("worker1", (long) (Math.random()*2000+3000), countDownLatch);
- Worker worker2 = new Worker("worker2", (long) (Math.random()*2000+3000), countDownLatch);
-
- worker0.start();
- worker1.start();
- countDownLatch.await();
-
- System.out.println("准备工作就绪");
- worker2.start();
-
- }
-
- }
观察控制台打印顺序,可以发现这种方法是可以模拟场景2的:
worker0开始工作
worker1开始工作
worker1第一阶段工作完成
worker0第一阶段工作完成
准备工作就绪
worker2开始工作
worker1第二阶段工作完成
worker1工作完成,耗费时间=5521
worker0第二阶段工作完成
worker0工作完成,耗费时间=6147
worker2第一阶段工作完成
worker2第二阶段工作完成
worker2工作完成,耗费时间=5384
最后,总结下CountDownLatch与join的区别:调用thread.join() 方法必须等thread 执行完毕,当前线程才能继续往下执行,而CountDownLatch通过计数器提供了更灵活的控制,只要检测到计数器为0当前线程就可以往下执行而不用管相应的thread是否执行完毕。