CyclicBarrier译为环形栅栏。参照JDK文档:
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally** wait for each other. The barrier is called cyclicbecause it can be re-used** after the waiting threads are released.
ACyclicBarrier
supports an optional[Runnable](https://link.zhihu.com/?target=https%3A//docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html)
command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. This barrier action is useful for updating shared-state before any of the parties continue.
CyclicBarrier是多线程之间同步的工具,适用于于多个线程互相等待,如果有一个线程没有达到barrier,其他线程会在barrier之前等待,然后一起越过barrier。
class Worker implements Runnable {
CyclicBarrier cyclicBarrier;
public Worker(CyclicBarrier cyclicBarrier) {
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
System.out.println(Thread.currentThread().getName() + "到达栅栏");
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "越过栅栏");
}
}
class BarrierAction implements Runnable {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() +"\t start");
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
System.out.println(Thread.currentThread().getName() +"\t end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
int num = 10;
CyclicBarrier cyclicBarrier = new CyclicBarrier(num, new BarrierAction());
for (int i = 0; i < num; i++) {
Thread thread = new Thread(new Worker(cyclicBarrier));
thread.start();
}
}
}
输出结果:
Thread-1到达栅栏
Thread-2到达栅栏
Thread-3到达栅栏
Thread-9到达栅栏
Thread-4到达栅栏
Thread-7到达栅栏
Thread-5到达栅栏
Thread-0到达栅栏
Thread-6到达栅栏
Thread-8到达栅栏
Thread-8 start
Thread-8 end
Thread-8越过栅栏
Thread-1越过栅栏
Thread-3越过栅栏
Thread-2越过栅栏
Thread-9越过栅栏
Thread-5越过栅栏
Thread-6越过栅栏
Thread-0越过栅栏
Thread-7越过栅栏
Thread-4越过栅栏
通过测试结果可知,cyclicBarrier由最后一个达到barrier的线程执行。