业务场景描述:假设一条流水线上有三个工作者:worker1,worker2,worker3。有一个任务的完成需要他们三者协作完成,worker3可以开始这个任务的前提是worker1和worker2完成了他们的工作,而worker1和worker2是可以并行他们各自的工作的。

1.join实现

public class CountDownLatchAndJoin {

    public static void main(String[] args) throws InterruptedException {

        // 三个独立的工人线程
worker worker1 = new worker("worker1", (long) (Math.random()*4000));
worker worker2 = new worker("worker2", (long) (Math.random()*4000));
worker worker3 = new worker("worker3", (long) (Math.random()*4000));
// worker worker1 = new worker("worker1", 6000);
// worker worker2 = new worker("worker2", 5000);
// worker worker3 = new worker("worker3", 5000);
worker1.start();
worker2.start(); worker1.join();
worker2.join();
System.out.println("准备工作就绪..."); worker3.start(); } // 工人类
public static class worker extends Thread {
// 名字
private String name;
//工作时间
private long time; worker(String name, long time) {
this.name = name;
this.time = time;
} public void run() {
try {
System.out.println(name + "开始工作");
Thread.sleep(time);
System.out.println(name + "工作完成,耗费时间=" + time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }

结果:

worker2开始工作
worker1开始工作
worker1工作完成,耗费时间=601
worker2工作完成,耗费时间=2886
准备工作就绪...
worker3开始工作
worker3工作完成,耗费时间=686

可以顺利的完成工作,join的工作原理是,不停检查thread是否存活,如果存活则让当前线程永远wait,直到thread线程终止,线程的notifyAll就会被调用,还可以理解为join就是插队的意思


2.CountDownLatch实现

public class CountDownLatchTest {

    public static void main(String[] args) throws InterruptedException {

        // 初始化计数器为2
CountDownLatch countDownLatch = new CountDownLatch(2); // 三个独立的工人线程
worker worker1 = new worker("worker1", (long) (Math.random() * 4000), countDownLatch);
worker worker2 = new worker("worker2", (long) (Math.random() * 4000), countDownLatch);
worker worker3 = new worker("worker3", (long) (Math.random() * 4000), countDownLatch);
worker1.start();
worker2.start(); // 当计数器不为0的时候均等待
countDownLatch.await(); System.out.println("准备工作就绪...");
worker3.start();
} public static class worker extends Thread { private String name;
private long time;
private CountDownLatch countDownLatch; worker(String name, long time, CountDownLatch countDownLatch) {
this.name = name;
this.time = time;
this.countDownLatch = countDownLatch;
} public void run() {
System.out.println(name + " 开始工作了。。。");
// 减一
countDownLatch.countDown();
System.out.println(name + " 工作完成。。。");
}
}
}

创建一个计数器为2的 CountDownLatch ,让Worker持有这个CountDownLatch 实例,当完成自己的工作后,调用countDownLatch.countDown() 方法将计数器减1。countDownLatch.await() 方法会一直阻塞直到计数器为0,主线程才会继续往下执行。

运行结果:

worker1 开始工作了。。。
worker1 工作完成。。。
worker2 开始工作了。。。
worker2 工作完成。。。
准备工作就绪...
worker3 开始工作了。。。
worker3 工作完成。。。

从结果上来看,都解决了问题,但是他们的区别在哪里呢?

业务场景:假设worker的工作可以分为两个阶段,work3 只需要等待work1和work2完成他们各自工作的第一个阶段之后就可以开始自己的工作了,而不是场景1中的必须等待work1和work2把他们的工作全部完成之后才能开始。这样join就不可以实现了,应当采用CountDownLatch 来实现。

public class CountDownLatchTest {

    public static void main(String[] args) throws InterruptedException {

        // 初始化计数器为5
CountDownLatch countDownLatch = new CountDownLatch(5); // 三个独立的工人线程
worker worker1 = new worker("worker1", (long) (Math.random() * 4000), countDownLatch);
worker worker2 = new worker("worker2", (long) (Math.random() * 4000), countDownLatch);
worker worker3 = new worker("worker3", (long) (Math.random() * 4000), countDownLatch);
worker worker4 = new worker("worker4", (long) (Math.random() * 4000), countDownLatch);
worker worker5 = new worker("worker5", (long) (Math.random() * 4000), countDownLatch);
worker worker6 = new worker("worker6", (long) (Math.random() * 4000), countDownLatch); worker1.start();
worker2.start();
worker3.start();
worker4.start();
worker5.start(); // 当计数器不为0的时候均等待
countDownLatch.await(); System.out.println("准备工作就绪...");
worker6.start();
} public static class worker extends Thread { private String name;
private long time;
private CountDownLatch countDownLatch; worker(String name, long time, CountDownLatch countDownLatch) {
this.name = name;
this.time = time;
this.countDownLatch = countDownLatch;
} public void run() {
try {
System.out.println(name + " 工作开始。。。");
Thread.sleep(time);
System.out.println(name + " 第一阶段工作完成。。。用时:" + time);
// 计数器减一
countDownLatch.countDown();
// 假设第二阶段的工作都需要两秒完成
Thread.sleep(2000);
System.out.println(name + " 第二阶段工作完成。。。用时:" + (time + 2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

多运行几次发现:线程6等到前面5个线程的第一阶段全部完成,就开始运行了,运行结果:

worker3 工作开始。。。
worker2 工作开始。。。
worker1 工作开始。。。
worker4 工作开始。。。
worker5 工作开始。。。
worker3 第一阶段工作完成。。。用时:1410
worker5 第一阶段工作完成。。。用时:2022
worker2 第一阶段工作完成。。。用时:2273
worker1 第一阶段工作完成。。。用时:2856
worker3 第二阶段工作完成。。。用时:3410
worker4 第一阶段工作完成。。。用时:3430
准备工作就绪...
worker6 工作开始。。。
worker5 第二阶段工作完成。。。用时:4022
worker2 第二阶段工作完成。。。用时:4273
worker1 第二阶段工作完成。。。用时:4856
worker4 第二阶段工作完成。。。用时:5430
worker6 第一阶段工作完成。。。用时:3773
worker6 第二阶段工作完成。。。用时:5773

总结:调用thread.join() 方法必须等thread 执行完毕,当前线程才能继续往下执行,而CountDownLatch通过计数器提供了更灵活的控制,只要检测到计数器为0当前线程就可以往下执行而不用管相应的thread是否执行完毕。

05-19 14:08