public CyclicBarrier(int parties) {
this(parties, null);
}
public CyclicBarrier(int parties, Runnable barrierAction) {
if (parties <= 0) throw new IllegalArgumentException();
this.parties = parties;
this.count = parties;
this.barrierCommand = barrierAction;
}
|
/** The lock for guarding barrier entry */
private final ReentrantLock lock = new ReentrantLock();
|
public int await() throws InterruptedException, BrokenBarrierException {
try {
return dowait(false, 0L);
} catch (TimeoutException toe) {
throw new Error(toe); // cannot happen
}
}
|
/**
* Main barrier code, covering the various policies.
*/
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 {
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.await();
else if (nanos > 0L)
nanos = trip.awaitNanos(nanos);
} catch (InterruptedException ie) {
if (g == generation && ! g.broken) {
breakBarrier();
throw ie;
} else {
// We're about to finish waiting even if we had not
// been interrupted, so this interrupt is deemed to
// "belong" to subsequent execution.
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();
}
}
|
private void nextGeneration() {
// signal completion of last generation
trip.signalAll();
// set up next generation
count = parties;
generation = new Generation();
}
/**
* Sets current barrier generation as broken and wakes up everyone.
* Called only while holding lock.
*/
private void breakBarrier() {
generation.broken = true;
count = parties;
trip.signalAll();
}
nextGeneration和breakBarrier方法均可以中止阻塞。
|
public void reset() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
breakBarrier(); // break the current generation
nextGeneration(); // start a new generation
} finally {
lock.unlock();
}
}
|
package com.fpc.Test;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.Random;
public class CyclicBarrierTest {
private CyclicBarrier cyclicBarrier = new CyclicBarrier(4);
private Random rnd = new Random();
class taskDemo implements Runnable{
private String taskId;
public taskDemo( String taskId ) {
this.taskId = taskId;
}
@Override
public void run() {
try {
int time = rnd.nextInt(1000);
Thread.sleep(time);
System.out.println(" Thread : " + taskId + " sleep : " + time + "ms");
try {
cyclicBarrier.await();
System.out.println(" Thread : " + taskId + " sleep is over");
} catch (BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// CyclicBarrier cyclicBarrier = new CyclicBarrier();
public static void main( String[] args ) {
CyclicBarrierTest c = new CyclicBarrierTest();
ExecutorService pool = Executors.newCachedThreadPool();
pool.submit(c.new taskDemo("1"));
pool.submit(c.new taskDemo("2"));
pool.submit(c.new taskDemo("3"));
pool.submit(c.new taskDemo("4"));
}
}
|
Thread : 1 sleep : 102ms
Thread : 3 sleep : 254ms
Thread : 4 sleep : 394ms
Thread : 2 sleep : 943ms
Thread : 2 sleep is over
Thread : 1 sleep is over
Thread : 4 sleep is over
Thread : 3 sleep is over
|
Thread : 3 sleep : 166ms
Thread : 4 sleep : 281ms
Thread : 2 sleep : 444ms
Thread : 1 sleep : 776ms
|
Thread : 3 sleep : 500ms
Thread : 1 sleep : 567ms
Thread : 2 sleep : 912ms
Thread : 2 sleep is over
Thread : 3 sleep is over
Thread : 1 sleep is over
Thread : 4 sleep : 988ms
|