Java Reference ยท Concurrency
CyclicBarrier and Phaser
Reference for CyclicBarrier and Phaser in java.util.concurrent: make threads wait for each other at a rendezvous point, round after round.
A barrier makes a group of threads wait for each other. Every thread does some work, arrives at the barrier and blocks until all the others have arrived; then all of them continue. Unlike CountDownLatch, a barrier is reusable, so it fits algorithms that work in rounds, such as simulations or phased computations.
CyclicBarrier has a fixed number of parties and optionally runs a barrier action each time the last party arrives. Phaser is the more flexible relative: the number of parties can change at runtime, and it tracks a phase number that increments each round.
CyclicBarrier barrier = new CyclicBarrier(3, () -> System.out.println("round done")); barrier.await(); // blocks until three threads called await() Phaser phaser = new Phaser(3); phaser.arriveAndAwaitAdvance(); // returns the next phase number
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
new CyclicBarrier(int parties) new CyclicBarrier(int parties, Runnable barrierAction) int await() throws InterruptedException, BrokenBarrierException int await(long timeout, TimeUnit unit) throws InterruptedException, BrokenBarrierException, TimeoutException new Phaser() / new Phaser(int parties) / new Phaser(Phaser parent, int parties)
| CyclicBarrier member | Description |
|---|---|
await() |
Waits for all parties. Returns the arrival index: getParties() - 1 for the first thread, 0 for the last. |
reset() |
Breaks the current round (waiting threads get BrokenBarrierException) and starts a new one. |
isBroken() |
true if a timeout, interrupt or reset broke the barrier. |
getNumberWaiting() |
Number of threads currently blocked at the barrier. |
| Phaser member | Description |
|---|---|
register(), bulkRegister(n) |
Adds parties dynamically. |
arrive() |
Signals arrival without waiting; returns the phase number. |
arriveAndAwaitAdvance() |
Arrives and blocks until the others arrive; returns the next phase. |
arriveAndDeregister() |
Arrives and removes this party. |
awaitAdvance(int phase) |
Waits for the given phase to end. |
onAdvance(int phase, int parties) |
Override to run code at each phase end; return true to terminate the phaser. |
Usage notes
- A broken barrier is contagious. If one waiting thread is interrupted or times out, all other waiting threads get
BrokenBarrierException. Handle that case, or the other threads end with an error. - The barrier action runs in the last thread to arrive, before anyone is released. Keep it short; an exception there breaks the barrier.
- The number of parties is fixed for
CyclicBarrier. If a thread dies before arriving, the others wait forever (or until their timeout). - Phaser removes that limit: parties can register and deregister at any time, and the number of registered parties never needs to match the thread count up front.
- Phaser termination: once
onAdvancereturnstrue, the phaser is terminated andarriveAndAwaitAdvance()returns a negative number. - Memory visibility: writes before
await()are visible to the other parties after they are released. - Use a
CountDownLatchwhen the waiting thread is different from the counting threads and you need it only once. - On a machine with one CPU the rounds still work; threads simply take turns.
Related
CountDownLatch,Semaphore,ForkJoinPool- Lessons: Synchronizers