countdownlatch 用法

//如下使用案例

package com.aresoft.test;

import java.util.concurrent.CountDownLatch;

public class Test {

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

CountDownLatch latch = new CountDownLatch(3);
for(int i = 0;i < 3; i++){
Thread t1 = new Thread(new HomeWork(latch));
t1.start();
System.out.println("1111");
}
//阻塞当前线程知道 直到调用此方法会一直阻塞当前线程,直到计时器的值为0
//当前线程是主线程。
latch.await();
System.out.println("caijue");
}
static class HomeWork implements Runnable{

public CountDownLatch latch;

public HomeWork(CountDownLatch latch){
this.latch = latch;
}

@Override
public void run() {
System.out.println("ssssss");
latch.countDown();
}

}

}

posted @ 2016-10-31 21:40  随风而逝,只是飘零  阅读(160)  评论(0编辑  收藏  举报