Differences between CountDownLatch and join

When the ants were asked this question, they really couldn't imagine the difference when they realized the same function. When they came back to Baidu, they found that it was actually a very simple problem.
_First of all, both can achieve blocking threads waiting to be completed before proceeding with subsequent logic. For the same functions as both, let's not repeat here. Let's just talk about the differences between them. Consider a scenario where our main thread is blocking somewhere and waiting for other threads to complete certain operations before proceeding with subsequent operations, specifically, main thread M, waiting for child threadsT1 and T2 complete an operation, but tasks in sub-threads T1 and T2 may have a lot to do, and when only one of the intermediate steps is completed, the main thread can actually wake up to continue execution without waiting for all the tasks of the sub-threads to finish before executing the main thread.In this case, if the main thread is blocked by calling the join method of the child thread, the main thread must wait until all tasks of the child thread have been executed before continuing to execute. In this case, efficiency is extremely low or even impossible, and CountDownLatch must be used to achieve finer-grained task control. The sample code is as follows:

  • Worker.java
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;  
    public void run() {  
        try {  
            System.out.println(name+"Phase 1 Work Completed");  
            Thread.sleep(2000); //Let's just assume that the second phase of the work takes two seconds to complete  
            System.out.println(name+"Phase 2 Work Completed");  
            System.out.println(name+"Work done, time consuming="+(time+2000));  
        } catch (InterruptedException e) {  
            // TODO Auto-generated catch Blocks  
  • Test.java
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);  
        System.out.println("Ready for Work");  

_The above code, work0 and work1, only needs to execute the first phase of the task main thread work2 to start execution. If you use join, you have to wait for all the tasks of work0 and work1 to finish executing. Wor2 needs to wait for two seconds without reason, so it is obviously more appropriate to use CountDownLatch at this time.
Reference resources: https://blog.csdn.net/nyistzp/article/details/51444487

Tags: Java

Posted on Mon, 02 Dec 2019 17:42:50 -0500 by curtis_b