一、CountDownLatch

主要用来解决一个线程等待多个线程的场景,计数器不能循环利用

public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 上完自习,离开教室");
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + " 班长最后关门走人");
    }
}

 

二、CyclicBarrier

是一组线程之间互相等待,计数器可以循环利用。

 

相关文章:

  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-15
  • 2022-12-23
  • 2021-11-10
  • 2021-11-27
  • 2021-07-08
相关资源
相似解决方案