多线程(八、同步计数器-CyclicBarrier)
CyclicBarrier-循环栅栏 线程到达栅栏时调用await方法被阻塞,只有线程数据达到栅栏设置的阈值时,栅栏放行,所有线程继续执行,此轮结束,栅栏进入下一轮。 案例:3个线程等待栅栏放行 Task import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class Task implements Runnable{ private CyclicBarrier cyclicBarrier; public Task(CyclicBarrier cyclicBarrier) { this.cyclicBarrier = cyclicBarrier; } @Override public void run() { try { Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + "准备就绪"); cyclicBarrier.await(); } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + "中断了......");