try { new CountDownLatchTest.Driver2().main(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
private static class Driver { // ... void main() throws InterruptedException { CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(5);
for (int i = 0; i < 5; ++i) // create and start threads new Thread(new Worker(startSignal, doneSignal)).start();
//执行dowork()方法前处理 doSomethingElse(); // don't let run yet //执行此方法后,dowork()执行 startSignal.countDown(); // let all threads proceed doSomethingElse1(); doneSignal.await(); // wait for all to finish }