参考文档:
https://blog.csdn.net/zxdfc/article/details/52752803
简介
CountDownLatch是一个同步辅助类。允许一个或多个线程等待其他线程完成操作。内部采用的公平锁和共享锁的机制实现
举个栗子
public class CountDownLatchTest { public static void main(String[] args) { CountDownLatch cd = new CountDownLatch(2); ExecutorService es = Executors.newFixedThreadPool(5); for (int i = 0; i < 2; i++) { es.execute(new MyThread1(cd)); } System.out.println("wait"); try { cd.await(); System.out.println("two thread run over"); } catch (InterruptedException e) { e.printStackTrace(); } es.shutdown(); } } class MyThread1 implements Runnable { CountDownLatch latch; public MyThread1(CountDownLatch latch) { this.latch = latch; } @Override public void run() { try { Thread.sleep(1000 * 3); latch.countDown(); System.out.println(Thread.currentThread().getName() + " over"); } catch (InterruptedException e) { e.printStackTrace(); } } }