Java code import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountDownLatchTest { public static void main(String[] args) { ExecutorService service = Executors.newCachedThreadPool(); final CountDownLatch startSignal = new CountDownLatch(1); final CountDownLatch doneSignal = new CountDownLatch(3); for(int i=0;i<3;i++){ Runnable runnable = new Runnable() { @Override public void run() { try { System.out.println("线程"+Thread.currentThread().getName()+"正在准备"); startSignal.await(); System.out.println("线程"+Thread.currentThread().getName()+"开始运行"); Thread.sleep((long) (Math.random() * 10000)); doneSignal.countDown(); System.out.println("线程"+Thread.currentThread().getName()+"已运行完"); } catch (InterruptedException e) { e.printStackTrace(); } } }; service.execute(runnable); } try { Thread.sleep((long) (Math.random() * 10000)); System.out.println("线程"+Thread.currentThread().getName()+"即将发出任务"); startSignal.countDown(); System.out.println("线程"+Thread.currentThread().getName()+"已发出任务,等待完成"); doneSignal.await(); System.out.println("所有任务都已完成"); } catch (InterruptedException e) { e.printStackTrace(); } } }