(Reproduced) CountDownLatch usage example

 

Reprinted: http://tomyz0223.iteye.com/blog/1018969 Author: tomyz0223

thanks~

CountDownLatch

 

CountDownlatch, is a Sychronizer that delays the thread's progress until the thread's progress reaches the terminated state.

 

It itself is a very useful class in Java concurrent packages. It allows some tasks to be completed before continuing to run the following content. After each task itself is executed, the counter is decremented by one until the counter is cleared. The following The content can continue to run, otherwise it will block waiting.

 

After thinking about it, this scenario is very suitable for such a scenario in the project: We have a project, which requires three third-party APIs, and gets the results. There is no problem in getting the results sequentially in a thread, but here is this The three tasks are very time-consuming operations. If the sequential acquisition performance is very poor, you can consider using three threads. When the three threads get the results, the main thread will continue to work. After the three threads have finished running, the main thread will be The thread goes to get the result of the child thread running. There is a very important premise here: our system runs on a server with 4 cpus, so that multi-threading can reflect performance, and the JVM will allocate these threads to run on different cpus as much as possible.

 

CountDownLatch has the following basic methods:

1) await(), blocking and waiting until the counter is cleared

 

2) await(int timeout, TimeUnit unit), which blocks the thread unless interrupted or the maximum waiting time is exceeded

await returns true if the counter is cleared, false if the wait exceeds the maximum wait time

 

3) countDown(), the counter is decremented by one, when the counter is cleared, the await thread is awakened, and the thread continues to execute

 

4) getCount  (), get the size of the current counter

 

 

package countdownlatch;

import java.util.concurrent.CountDownLatch;

/**
 *
 *<p>Test</p>
 *<p>Description:</P>
 *<p>Company:</p>
 *<p>Department:CAS</p>
 *@Author: Tommy Zhou
 *@Since: 1.0
 *@Version:Date:2011-4-26
 *
 **/

public class CountDownLatchSample {
    
    public static void main(String[] args) {
        String[] strs = getResult();
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
        
    }
    
    public static String[] getResult(){
        String[] strs = new String[3];
        CountDownLatch countDownLatch = new CountDownLatch(3);
        Work1 work1 = new Work1(countDownLatch,strs[0]);
        Work2 work2 = new Work2(countDownLatch,strs[1]);
        Work3 work3 = new Work3(countDownLatch,strs[2]);
        work1.start();
        work2.start();
        work3.start();
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        strs[0] = work1.str1;
        strs[1] = work2.str2;
        strs[2] = work3.str3;
        return strs;
    }
}

class Work1 extends Thread{
    public String str1;
    public CountDownLatch latch1;
    public Work1(CountDownLatch latch1,String str1){
        this.latch1 = latch1;
        this.str1 = str1;
    }
    
    
    public void run(){
        str1="work1";
        latch1.countDown();
    }
}

class Work2 extends Thread{
    public String str2;
    public CountDownLatch latch2;
    public Work2(CountDownLatch latch2,String str2){
        this.latch2 = latch2;
        this.str2 = str2;
    }
    
    
    public void run(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        str2="work2";
        latch2.countDown();
    }
}

class Work3 extends Thread{
    public String str3;
    public CountDownLatch latch3;
    public Work3(CountDownLatch latch3,String str3){
        this.latch3 = latch3;
        this.str3 = str3;
    }
    
    
    public void run(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        str3="work3";
        latch3.countDown();
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326655053&siteId=291194637