参考文章:http://www.jianshu.com/p/0c2af47f98fd
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/**
*@className CyclicBarrierTest
*@description :
*
*
* CyclicBarrier是同步辅助类,它允许一组线程相互等待,直到到达某个公共屏障点。
*
*
* 重要属性:
* 锁对象 lock
private final ReentrantLock lock = new ReentrantLock();
trip 意思是 直到出错之前的条件
private final Condition trip = lock.newCondition();
参与者
private final int parties;
barrier将执行的任务或者命令
private final Runnable barrierCommand;
generation对象。Generation是其内部类
private Generation generation = new Generation();
private int count;
* 构造参数如下:
* CyclicBarrier(int parties);
* CyclicBarrier(int parties, Runnable barrierAction);
* if (parties <= 0) throw new IllegalArgumentException();
this.parties = parties;
this.count = parties;
this.barrierCommand = barrierAction;
*
* CyclicBarrier里面的内部类,它表示当前的CyclicBarrier是否是被损坏的。默认broken为false是完整的。
* private static class Generation {boolean broken = false;}
*
*
*
* 重要方法:
* 1.nextGeneration()它里面有三个操作,
* trip.signalAll();唤醒所有线程
count = parties;
generation = new Generation();重新生成Generation对象
*
* 2.breakBarrier() 破坏CyclicBarrier
* generation.broken = true;
count = parties;
trip.signalAll();
* .await(),它的内部是调用dowait(false, 0L);这里表示 不超时限制。
*
* 3.dowait(boolean timed, long nanos)这里的参数是:对于是否超时,及超时的限制。
*
* dowait的内部实现是:
* final ReentrantLock lock = this.lock;
lock.lock();
try {
final Generation g = generation; 获取CyclicBarrier的属性
if (g.broken)
throw new BrokenBarrierException();
if (Thread.interrupted()) {
breakBarrier();
throw new InterruptedException();
}
int index = --count; 指定线程数量--
if (index == 0) {
boolean ranAction = false;
try {
final Runnable command = barrierCommand;
if (command != null) 如果创建对象指定barrierAction,将执行run方法
command.run();
ranAction = true;
nextGeneration();
return 0;
} finally {
if (!ranAction)
breakBarrier();
}
}
for (;;) {
try {
if (!timed)
trip.await();
else if (nanos > 0L)
nanos = trip.awaitNanos(nanos);
} catch (InterruptedException ie) {
if (g == generation && ! g.broken) {
breakBarrier();
throw ie;
} else {
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(); finally释放锁,这点要注意
}
*
*
*@date 2017年6月15日上午9:54:03
*
*/
public class CyclicBarrierTest {
private static CyclicBarrier cBarrier = null;
public static void main(String[] args) {
cBarrier = new CyclicBarrier(5,new Runnable() {
@Override
public void run() {
System.out.println("人到齐了,开会吧");
}
});
for(int i=0;i<5;i++){
Thread party = new PartiesThread();
/*if(i == 4){
party.interrupt();
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}*/
party.start();
}
}
static class PartiesThread extends Thread{
public void run(){
System.out.println(Thread.currentThread().getName()+"到了会议室");
try {
cBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
}
}