读CyclicBarrier源码


//一个循环的屏障。所有的线程在屏障处等待其他线程执行完毕。然后再各自执行。
//先看构造函数
public CyclicBarrier(int parties) {
this(parties, null);
}


//barrierAction代表在屏障上等待的最后一个线程已经执行完后,执行的runnable
public CyclicBarrier(int parties, Runnable barrierAction) {
if (parties <= 0) throw new IllegalArgumentException();
this.parties = parties;
this.count = parties;
this.barrierCommand = barrierAction;
}


//等待其他线程执行到这
public int await() throws InterruptedException, BrokenBarrierException {
try {
return dowait(false, 0L);
} catch (TimeoutException toe) {
throw new Error(toe); // cannot happen;
}
}


//如果count-1不为0则线程在trip条件上等待。否则唤醒在trip条件上等待的线程。
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) { // tripped
boolean ranAction = false;
try {
//执行runnable
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
for (;;) {
try {
if (!timed)
//在trip条件上等待
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();
}
}

//中断此barrier将所有在trip条件上等待的线程加入唤醒的队列
private void breakBarrier() {
generation.broken = true;
count = parties;
trip.signalAll();
}


//重新生成下一代CyclicBarrier
private void nextGeneration() {
//唤醒在trip上等待的所有线程
trip.signalAll();
// set up next generation
//设置下一代操作(重用CyclicBarrier)
count = parties;
generation = new Generation();
}

//在此时间段等待
public int await(long timeout, TimeUnit unit)
throws InterruptedException,
BrokenBarrierException,
TimeoutException {
return dowait(true, unit.toNanos(timeout));
}

//是否已经被损坏
public boolean isBroken() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return generation.broken;
} finally {
lock.unlock();
}
}

//返回要求启动此CyclicBarrier的参与者数量。
public int getParties() {
return parties;
}

//还有几个参与者在等待
public int getNumberWaiting() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return parties - count;
} finally {
lock.unlock();
}
}

//损坏旧屏障。创建新一代
public void reset() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
breakBarrier(); // break the current generation
nextGeneration(); // start a new generation
} finally {
lock.unlock();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值