JDK1.8并发包之 -- CyclicBarrier

前文讲述CountDownLatch,详见 JDK1.8并发包之 -- CountDownLatch,本文讲述与之类似的并发类--CyclicBarrier,中文叫可循环屏障。

1、第一段,实现目标
 * 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
 * cyclic because it can be re-used after the waiting threads
 * are released.

CyclicBarrier的同步器的目的是让一组多线程相互等待,共同到达一个屏障点,CyclicBarrier经常用在一组固定线程数的场景中,需要互相等待。屏障的作用是让先到达的线程守候在这里,全部线程都到达后,cyclic方法是可循环的,说明可以被重复利用。可重复利用这点是CountDownLatch不具备的。

2、第二段,构造方法
 * A {@code CyclicBarrier} supports an optional {@link Runnable} 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的构造方法中能传入一个Runnable对象,最后一个任务线程到达屏障点后,再线程被唤醒前,最后一个线程执行一次Runnable的run方法,barrier非常有用的是在多线程被唤醒前会更新共享状态,这就是可循环利用的原因。

3、第三段、案例

类注释中的代码不是非常清晰,笔者自己写了一段案例。

@Slf4j
public class CyclicBarrierDemo {


    public static void main(String[] args) throws InterruptedException {
        int num = 10;
        CyclicBarrier barrier = new CyclicBarrier(num, new Runnable() {
            @Override
            public void run() {
                log.info("所有年级达到操场,开始升国旗...");
            }
        });

        for (int i=0; i

运行结果如下,显而易见,10个班级都到达操场集合后,才能开始升旗活动。且可重复利用。

15:52:16.321 [Thread-11] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级2 到达操场...
15:52:16.320 [Thread-14] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级5 到达操场...
15:52:16.320 [Thread-4] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级5 到达操场...
15:52:16.321 [Thread-0] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级1 到达操场...
15:52:16.321 [Thread-12] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级3 到达操场...
15:52:16.321 [Thread-16] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级7 到达操场...
15:52:16.321 [Thread-10] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级1 到达操场...
15:52:16.320 [Thread-3] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级4 到达操场...
15:52:16.321 [Thread-2] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级3 到达操场...
15:52:16.321 [Thread-8] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级9 到达操场...
15:52:16.328 [Thread-8] INFO sw.melody.concurrent.CyclicBarrierDemo - 所有年级达到操场,开始升国旗...
15:52:16.322 [Thread-17] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级8 到达操场...
15:52:16.322 [Thread-7] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级8 到达操场...
15:52:16.322 [Thread-6] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级7 到达操场...
15:52:16.322 [Thread-9] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级10 到达操场...
15:52:16.322 [Thread-13] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级4 到达操场...
15:52:16.322 [Thread-1] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级2 到达操场...
15:52:16.322 [Thread-5] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级6 到达操场...
15:52:16.322 [Thread-18] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级9 到达操场...
15:52:16.322 [Thread-15] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级6 到达操场...
15:52:16.322 [Thread-19] INFO sw.melody.concurrent.CyclicBarrierDemo - 班级10 到达操场...
15:52:16.345 [Thread-19] INFO sw.melody.concurrent.CyclicBarrierDemo - 所有年级达到操场,开始升国旗...
4、第四段
 * 

If the barrier action does not rely on the parties being suspended when * it is executed, then any of the threads in the party could execute that * action when it is released. To facilitate this, each invocation of * {@link #await} returns the arrival index of that thread at the barrier. * You can then choose which thread should execute the barrier action, for * example: *

 {@code
 * if (barrier.await() == 0) {
 *   // log the completion of this iteration
 * }}

这段话的意思是,如果多线程执行的过程中不会暂停互相等待,那么任何被唤醒的线程都可以执行屏障线程的任务。await方法实际上返回的屏障数量的序列号,可以自由判断哪个序列号执行屏障线程任务。如果我们不去重写CyclicBarrier代码,那么这段本身没有意思。

5、第五段,执行异常
 * 

The {@code CyclicBarrier} uses an all-or-none breakage model * for failed synchronization attempts: If a thread leaves a barrier * point prematurely because of interruption, failure, or timeout, all * other threads waiting at that barrier point will also leave * abnormally via {@link BrokenBarrierException} (or * {@link InterruptedException} if they too were interrupted at about * the same time).

CyclicBarrier屏障处理失败,可能由于线程调用interruption方法,执行异常,或者超时,那么等待在屏障点的线程就会抛出BrokenBarrierException。

你可能感兴趣的:(JDK1.8并发包之 -- CyclicBarrier)