public class CyclicBarrier {
private static class Generation {
boolean broken = false ;
}
private final ReentrantLock lock = new ReentrantLock();
private final Condition trip = lock .newCondition();
private final int parties;
private final Runnable barrierCommand;
private Generation generation = new Generation();
private int count;
private void nextGeneration () {
trip.signalAll();
count = parties;
generation = new Generation();
}
private void breakBarrier () {
generation.broken = true ;
count = parties;
trip.signalAll();
}
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 ) {
boolean ranAction = false ;
try {
final Runnable command = barrierCommand;
if (command != null )
command.run();
ranAction = true ;
nextGeneration();
return 0 ;
} finally {
if (!ranAction)
breakBarrier();
}
}
for (;;) {
try {
if (!timed)
trip.await ();
else if (nanos > 0 L)
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 <= 0 L) {
breakBarrier();
throw new TimeoutException();
}
}
} finally {
lock .unlock();
}
}
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 );
}
public int getParties () {
return parties;
}
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();
}
}
public void reset () {
final ReentrantLock lock = this .lock ;
lock .lock ();
try {
breakBarrier();
nextGeneration();
} finally {
lock .unlock();
}
}
public int getNumberWaiting () {
final ReentrantLock lock = this .lock ;
lock .lock ();
try {
return parties - count;
} finally {
lock .unlock();
}
}
}
public class Main {
static class Ruuner implements Runnable {
private CyclicBarrier barrier;
private String name;
public Ruuner (String name, CyclicBarrier barrier) {
this .name = name;
this .barrier = barrier;
}
@Override
public void run () {
try {
Thread.sleep(100 * (new Random()).nextInt(100 ));
System.out .println(name + "准备好了." );
barrier.await ();
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (BrokenBarrierException bbe) {
bbe.printStackTrace();
}
System.out .println(name + "出发." );
}
}
public static void main (String[] arg) {
CyclicBarrier barrier = new CyclicBarrier(3 );
Thread t1 = new Thread(new Ruuner("1号" ,barrier));
Thread t2 = new Thread(new Ruuner("2号" ,barrier));
Thread t3 = new Thread(new Ruuner("3号" ,barrier));
t1.start();
t2.start();
t3.start();
}
}