CountDownLatch的使用实例
CountDownLatch
?
CountDownlatch,是一种Sychronizer,它可以延迟线程的进度直到线程的进度到线程到达终止状态。
?
它本身而言是Java并发包中非常有用的一个类,它可以让某些任务完成以后再继续运行下面的内容,每个任务本身执行完毕后让计数器减一,直到计数器清零后,以下的内容才可以继续运行,否则将阻塞等待。
?
想了一下,这个场景非常适合用于项目中这样的场景:我们有个项目,它需要三个第三方的API,并把结果拿到,在一个线程中顺序去拿结果没有问题,但是这里这三个任务是非常耗时的操作,如果顺序获取性能非常差,因此可以考虑用三个线程,当三个线程拿到结果后才继续主线程的工作,等三个线程运行结束后,由主线程去取子线程运行的结果。这里有个很重要的前提:我们的系统运行在4个cpu的server上,这样多线程才能体现性能,JVM会分配这些线程尽量运行在不同的cpu上。
?
CountDownLatch有以下基本方法:
1)await(),阻塞等待,直到计数器清零
?
2)await(int timeout, TimeUnit unit),使线程阻塞,除非被中断或者超过等待的最大时间
如果达到计数器清零,则await返回true,如果等待超过了最大的等待时间,则返回false
?
3)countDown(),计数器减一,当计数器清零时,await的线程被唤醒,线程继续执行
?
4)getCount(),获取当前计数器的大小
?
?
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();
??? }
}