是什么
字面意思是“可重复使用的栅栏”或“周期性的栅栏”,总之不是用了一次就没用了的。
怎么用

看图我们也知道了,CyclicBarrier 的源码最重要的就是 await() 方法了。
初始化 CyclicBarrier
public CyclicBarrier(int parties, Runnable barrierAction) {if (parties <= 0) throw new IllegalArgumentException();this.parties = parties;this.count = parties;this.barrierCommand = barrierAction;}/*** Creates a new {@code CyclicBarrier} that will trip when the* given number of parties (threads) are waiting upon it, and* does not perform a predefined action when the barrier is tripped.** @param parties the number of threads that must invoke {@link #await}* before the barrier is tripped* @throws IllegalArgumentException if {@code parties} is less than 1*/public CyclicBarrier(int parties) {this(parties, null);}
上面的构造函数中除了初始化 parties 和 count 以及 barrierCommand 以外,CyclicBarrier 还提前创建好了一些变量。
// CyclicBarrier 的等待线程通过 barrier 的“条件”是大家都到了栅栏上private final Condition trip = lock.newCondition();private static class Generation {boolean broken = false;}private Generation generation = new Generation();
await() 函数
public int await() throws InterruptedException, BrokenBarrierException {try {return dowait(false, 0L);} catch (TimeoutException toe) {throw new Error(toe); // cannot happen}}
private int dowait(boolean timed, long nanos)throws InterruptedException, BrokenBarrierException,TimeoutException {final ReentrantLock lock = this.lock;lock.lock();try {final Generation g = generation;if (g.broken)throw new BrokenBarrierException();if (Thread.interrupted()) {breakBarrier();throw new InterruptedException();}int index = --count;if (index == 0) { // trippedboolean ranAction = false;try {final Runnable command = barrierCommand;if (command != null)command.run();ranAction = true;// 唤醒等待的线程,然后开启新的一代nextGeneration();return 0;} finally {if (!ranAction)breakBarrier();}}// loop until tripped, broken, interrupted, or timed out// 如果是最后一个线程调用 await,那么上面就返回了// 下面的操作是给那些不是最后一个到达栅栏的线程执行的for (;;) {try {// 如果带有超时机制,调用带超时的 Condition 的 await 方法等待,直到最后一个线程调用 awaitif (!timed)trip.await();else if (nanos > 0L)nanos = trip.awaitNanos(nanos);} catch (InterruptedException ie) {if (g == generation && ! g.broken) {breakBarrier();throw ie;} else {// We're about to finish waiting even if we had not// been interrupted, so this interrupt is deemed to// "belong" to subsequent execution.Thread.currentThread().interrupt();}}if (g.broken)throw new BrokenBarrierException();if (g != generation)return index;if (timed && nanos <= 0L) {breakBarrier();throw new TimeoutException();}}} finally {lock.unlock();}}
看怎么开启新的一代:
// 开启新的一代,当最后一个线程到达栅栏上的时候,调用这个方法来唤醒其他线程,同时初始化“下一代”
private void nextGeneration() {
// 首先,需要唤醒所有的在栅栏上等待的线程
trip.signalAll();
// 更新 count 的值
count = parties;
// 重新生成“新一代”
generation = new Generation();
}
开启新的一代,类似于重新实例化一个 CyclicBarrier 实例
看看怎么打破一个栅栏:
private void breakBarrier() {
// 设置状态 broken 为 true
generation.broken = true;
// 重置 count 为初始值 parties
count = parties;
// 唤醒所有已经在等待的线程
trip.signalAll();
}
总结
在初始化 CyclicBarrier 时,会初始化线程数 count。然后每次线程 t 执行 await() 时,会把这个线程放入到条件队列里面,并且令 count - 1。当 count = 0 时,会执行一个 runnable(也是我们传入的参数) 方法,接着开启新的一代,此时会调用 signalAll() 方法唤醒在等待队列里的所有线程。
