import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* CountDownLatch示例,模拟发令员倒数3个数,5个运动员一起跑
* @author Administrator
*
*/
public class CountdownDemo {
public static void main(String[] args) throws Exception {
final CountDownLatch cdl=new CountDownLatch(3);
Runnable ru=() -> {
String name=Thread.currentThread().getName();
System.out.println(name+",ready");
try {
cdl.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name+",running");
};
ExecutorService es=Executors.newFixedThreadPool(5);
es.submit(ru);
es.submit(ru);
es.submit(ru);
es.submit(ru);
es.submit(ru);
do {
Thread.sleep(200);
cdl.countDown();
}while(cdl.getCount()>0);
System.out.println("go~~~~");
es.shutdown();
}
}
输出结果