首页 > 代码库 > CountDownLatchTest

CountDownLatchTest

import java.util.concurrent.CountDownLatch;

/**
* 出发点:等待所有线程执行完成
* @author yinchuan.chen
*
*/
public class CountDownLatchTest {

public static void main(String[] args) throws InterruptedException {
CountDownLatch cdl = new CountDownLatch(4);
for(int i = 0; i < 4; i++) {
final int count = i;
Thread t = new Thread(new Runnable() {

public void run() {
System.out.println(count);
cdl.countDown();
}
});

t.start();
}

cdl.await();
System.out.println("等所有现场执行完成,才打印");

}
}

CountDownLatchTest