public int await() throws InterruptedException, BrokenBarrierException {try{return dowait(false, 0L);
}catch(TimeoutException toe) {throw new Error(toe); //cannot happen
}
}private int dowait(boolean timed, longnanos)
throws InterruptedException, BrokenBarrierException,
TimeoutException {
final ReentrantLocklock = this.lock;lock.lock(); // 1try{
final Generation g=generation;if(g.broken)throw newBrokenBarrierException();if(Thread.interrupted()) {
breakBarrier();throw newInterruptedException();
}int index = --count; // 2if (index == 0) { // 3
boolean ranAction = false;try{
final Runnable command=barrierCommand;if (command != null)
command.run();
ranAction= true;
nextGeneration(); // 4return 0;
}finally{if (!ranAction)
breakBarrier(); // 5
}
}//loop until tripped, broken, interrupted, or timed out
for(;;) {try{if (!timed)
trip.await(); // 6else if (nanos > 0L)
nanos=trip.awaitNanos(nanos);
}catch(InterruptedException ie) {if (g == generation && !g.broken) {
breakBarrier();throwie;
}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 newBrokenBarrierException();if (g !=generation)returnindex;if (timed && nanos <= 0L) {
breakBarrier();throw newTimeoutException();
}
}
}finally{lock.unlock(); // 7
}
}