, - Gate, CountDownLatch. .
gate.ready(), gate.go()
, 1 . , go .
final class Gate {
final CountDownLatch ready;
final CountDownLatch go = new CountDownLatch(1);
Gate(final int threads) {
ready = new CountDownLatch(threads);
}
void ready() {
ready.countDown();
await(go);
}
void go() {
await(ready);
go.countDown();
}
static void await(final CountDownLatch latch) {
try {
if (!latch.await(5, TimeUnit.SECONDS)) {
throw new TimedOutException()
}
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
}
static final class TimedOutException extends IllegalStateException {}
}
, , , - Doug Lea Phaser, Java7.