class Driver { // ...
public static void main(String[] argus) throws InterruptedException {
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(3);
for (int i = 0; i < 3; ++i) // create and start threads
new Thread(new Worker2(startSignal, doneSignal)).start();
System.out.println(Thread.currentThread().getName()+1); // don't let run yet
startSignal.countDown(); // let all threads proceed
Thread.sleep(new Random().nextInt(3000));
System.out.println(Thread.currentThread().getName()+2);
// doneSignal.countDown();
// doneSignal.countDown();
// doneSignal.countDown();
doneSignal.await(); // wait for all to finish 在此处堵塞,只有计数器减为零才继续
System.out.println("over");
}
}