java并发初探CyclicBarrier
CyclicBarrier的作用
CyclicBarrier,“循环屏障”的作用就是一系列的线程等待直至达到屏障的“瓶颈点”。
具体的使用就是就是一些列的线程调用CyclicBarrier得await()方法进入等待,直至达到
CyclicBarrier设置的barrier size,其他的线程会继续运行。
/**
* 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
* <em>cyclic</em> because it can be re-used after the waiting threads
* are released.
*/
CyclicBarrier源码
成员变量
/** The lock for guarding barrier entry */
private final ReentrantLock lock = new ReentrantLock();
/** Condition to wait on until tripped */
private final Condition trip = lock.newCondition();
/** The number of parties */
private final int parties;
/* The command to run when tripped */
private final Runnable barrierCommand;
/** The current generation */
private Generation generation = new Generation();
构造器
public CyclicBarrier(int parties, Runnable barrierAction) {
if (parties <= 0) throw new IllegalArgumentException();
this.parties = parties;
this.count = parties;
this.barrierCommand = barrierAction;
}
public CyclicBarrier(int parties) {
this(parties, null);
}
例子
package com.java.javabase.thread.base.concurrent.lock;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/**
* @author
*/
@Slf4j
public class CyclicBarrierTest {
public static int size = 5;
private static CyclicBarrier cyclicBarrier = new CyclicBarrier(size
, () -> log.info("thread :{} run after barrier broken", Thread.currentThread().getName()));
public static void main(String[] args) {
for (int i = 0; i < size; i++) {
new Thread() {
@Override
public void run() {
try {
log.info("thread : {}, begin", Thread.currentThread().getName());
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
log.info("thread : {}, end", Thread.currentThread().getName());
}
}.start();
}
}
}
例子结果
2019-08-12 11:04:07,511 [Thread-0] INFO CyclicBarrierTest - thread : Thread-0, begin
2019-08-12 11:04:07,511 [Thread-2] INFO CyclicBarrierTest - thread : Thread-2, begin
2019-08-12 11:04:07,511 [Thread-4] INFO CyclicBarrierTest - thread : Thread-4, begin
2019-08-12 11:04:07,511 [Thread-1] INFO CyclicBarrierTest - thread : Thread-1, begin
2019-08-12 11:04:07,511 [Thread-3] INFO CyclicBarrierTest - thread : Thread-3, begin
2019-08-12 11:04:07,511 [Thread-3] INFO CyclicBarrierTest - thread :Thread-3 run after barrier broken
2019-08-12 11:04:07,511 [Thread-3] INFO CyclicBarrierTest - thread : Thread-3, end
2019-08-12 11:04:07,511 [Thread-0] INFO CyclicBarrierTest - thread : Thread-0, end
2019-08-12 11:04:07,511 [Thread-2] INFO CyclicBarrierTest - thread : Thread-2, end
2019-08-12 11:04:07,511 [Thread-4] INFO CyclicBarrierTest - thread : Thread-4, end
2019-08-12 11:04:07,511 [Thread-1] INFO CyclicBarrierTest - thread : Thread-1, end