CountDownLatch 是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行。

使用示例

public class Test {
    public static void main(String[] args) throws Exception{
        CountDownLatch count = new CountDownLatch(3); //倒计时器
        for(int i=1; i<=3; i++) {
           new Thread(() -> {
               System.out.println(Thread.currentThread().getName());
               count.countDown(); //减一
           }, "Thread-"+i).start();
        }
        count.await(); //count归零后才能继续执行下去
        System.out.println("结束!");
    }
}

结果:
Thread-1
Thread-3
Thread-2
结束!

相关文章:

  • 2022-01-14
  • 2021-03-27
  • 2018-07-26
  • 2019-08-07
  • 2020-05-14
  • 2018-03-08
  • 2022-12-23
猜你喜欢
  • 2021-08-11
  • 2021-10-11
  • 2022-02-22
相关资源
相似解决方案