CountDownLatch能够让所有线程在某一个点阻塞,等待CountDownLatch的Count为0时才继续。
package com.skydream.thread.countDownLatch;
import java.util.concurrent.CountDownLatch;
public class CountDownLatchTest {
/**
* @param args
*/
public static void main(String[] args) {
final CountDownLatch countDownLatch = new CountDownLatch(3);
for(int i=0;i<2;i++)
{
new Thread(){
public void run() {
try {
System.out.println("awaiting......");
countDownLatch.await();
System.out.println("executor.......");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
new Thread(){
public void run() {
System.out.println("the other Thread");
try {
countDownLatch.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("executor222.......");
}
}.start();
for(int i=0;i<5;i++)
{
try {
Thread.sleep(5000);
countDownLatch.countDown();
System.out.println("countDownLatch number:"+countDownLatch.getCount());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}