首页javaconcurrentJava Thread - 如何比较倒计数锁存与循环障碍

Java Thread - 如何比较倒计数锁存与循环障碍

我们想知道如何比较倒计数锁存与循环障碍。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Main {
  static CyclicBarrier barrier = new CyclicBarrier(3);
  public static void main(String[] args) throws InterruptedException {
    new Worker().start();
    Thread.sleep(1000);
    new Worker().start();
    Thread.sleep(1000);
    new Worker().start();
    Thread.sleep(1000);

    System.out.println("Barrier automatically resets.");

    new Worker().start();
    Thread.sleep(1000);
    new Worker().start();
    Thread.sleep(1000);
    new Worker().start();
  }
}

class Worker extends Thread {
  @Override
  public void run() {
    try {
      Main.barrier.await();
      System.out.println("Let's play.");
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (BrokenBarrierException e) {
      e.printStackTrace();
    }
  }
}