Quick revision
The short answer
A Phaser is a reusable barrier whose number of parties can change while it runs, and which counts phases as it goes. register() adds a party, arriveAndDeregister() arrives and removes that party, arriveAndAwaitAdvance() arrives and waits for all registered parties like a barrier, and arrive() signals without waiting, which allows latch-like use. Overriding onAdvance() runs code between phases, and returning true from it terminates the phaser.
What to remember
In this answer, we saw that a Phaser extends the latch and barrier ideas with a changing number of parties and numbered phases that can be reused. register() adds a party and arriveAndDeregister() arrives and removes it; arriveAndAwaitAdvance() arrives and waits like a barrier; arrive() signals without waiting, which with awaitAdvance() gives latch-like use; and onAdvance() runs between phases and terminates the phaser when it returns true, which by default happens once no parties remain.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A Phaser is a reusable barrier whose number of parties can change while it runs, and which counts phases as it goes. register() adds a party, arriveAndDeregister() arrives and removes that party, arriveAndAwaitAdvance() arrives and waits for all registered parties like a barrier, and arrive() signals without waiting, which allows latch-like use. Overriding onAdvance() runs code between phases, and returning true from it terminates the phaser.
Why this question is asked
CountDownLatch and CyclicBarrier each solve one shape of problem: wait once for N events, or make a fixed group meet repeatedly. Real programs often need something in between, such as a group whose members come and go, or a coordinator that signals progress without waiting itself. Phaser covers all of these with one class, which is why interviewers use it to check whether we understand synchronizers in general rather than memorising each one. In this answer, we will recap what the older two classes cannot do, look at the Phaser's model of parties and phases, go through each of the four methods with code, and finish with onAdvance() and termination.
1What Latches and Barriers Cannot Do
A CountDownLatch is created with a count that can only go down, and it is one-shot: after it reaches zero, it cannot be used again. A CyclicBarrier can be reused for many phases, but the number of parties is fixed in its constructor. If a fourth worker wants to join a three-party barrier, or one of three wants to leave early, there is no way to tell the barrier.
A Phaser removes both limits. The number of parties is a live count that can go up and down between and during phases, and the phaser can be reused for as many phases as we like, numbering them 0, 1, 2 and so on.
| Feature | CountDownLatch | CyclicBarrier | Phaser |
|---|---|---|---|
| Reusable for many phases | no | yes | yes |
| Parties can change | no | no | yes, register and deregister |
| Signal without waiting | countDown() | no | arrive() |
| Code between phases | no | barrier action | onAdvance() |
| Phase number available | no | no | yes, getPhase() |
2The Model: Parties, Arrivals and Phases
A Phaser keeps three numbers: the current phase, how many parties are registered, and how many of them have arrived in this phase. A party is just a registration; it is not tied to a thread, although usually each participating thread registers once. When the number of arrived parties equals the number of registered parties, the phase advances: the phase number goes up by one, onAdvance() is called, and every thread waiting for that phase is released. The arrival count then starts again from zero for the new phase.
Every arrival method returns the phase number it arrived in, and awaitAdvance(phase) waits until the phaser has moved past a given phase. Those two pieces are the building blocks of everything else.
3Register() and arriveAndDeregister()
register() adds one party to the phaser; bulkRegister(n) adds several; the constructor new Phaser(n) starts with n registered. A party can join at any time, and it takes part from the current phase onwards. arriveAndDeregister() is the way to leave: it counts as an arrival for the current phase and then removes the party, so the next phase needs one fewer arrival. A party that leaves without deregistering would block every later phase forever, so leaving correctly matters.
Notice that the main thread is one of the parties. It registered itself through the constructor, waits with the workers through phase 0, and deregisters in phase 1. When the last party deregisters, the number of registered parties drops to zero, and by default the phaser terminates, as we will see with onAdvance().
4ArriveAndAwaitAdvance() and arrive()
arriveAndAwaitAdvance() is the barrier operation: it records this party's arrival and blocks until every registered party has arrived in the same phase. It is the equivalent of CyclicBarrier.await().
arrive() records an arrival and returns immediately, without waiting. That splits the barrier into two halves: a party can say I am done with this phase, and move on to other work. Combined with awaitAdvance(phase), it lets a coordinator wait for workers without being a worker itself, which is how a Phaser can act like a reusable latch.
5 messages in order. Play them, or step through them one at a time.
The difference from a latch is that the Phaser is now in phase 1 and can be used again for the next round of three arrivals. awaitAdvance() does not count as an arrival, so the coordinator is not a party at all. When a wait must be bounded, awaitAdvanceInterruptibly(phase, timeout, unit) is the timed and interruptible form of awaitAdvance().
5OnAdvance() and Termination
onAdvance(phase, registeredParties) is a protected method that runs once each time the phase advances, in the thread of the last party to arrive, before any waiting thread is released. It is the Phaser's version of the barrier action. Its return value matters: returning true terminates the phaser. The default implementation returns true when registeredParties is zero, which is why the phaser in the first example terminated when its last party left.
Overriding it lets us run code between phases and stop after a fixed number of rounds.
Once terminated, the phaser releases every waiting thread, and all further arrival and wait methods return immediately with a negative phase number. Workers can loop with a condition such as while (!phaser.isTerminated()), which gives a clean way to finish a multi-round computation from one place.
6Follow-up questions
Is there a limit on the number of parties?
Yes. A single phaser can have at most 65,535 registered parties at once; registering more throws IllegalStateException. For more, phasers can be arranged in a tree with new Phaser(parent), where each child counts as one party of its parent, which also reduces contention.
What happens if a waiting thread is interrupted?
arriveAndAwaitAdvance() is not interruptible: it keeps waiting if the thread is interrupted, unlike CyclicBarrier. If we need interruption or a timeout, we call arrive() and then awaitAdvanceInterruptibly(phase, timeout, unit). Also unlike a barrier, one party giving up does not break the phaser for the others.
When would you still choose CyclicBarrier?
When the group is fixed and we want its all-or-nothing failure behaviour, where one party's timeout or interrupt releases everyone with BrokenBarrierException. A Phaser has no broken state, so failure handling must be designed separately.
7Conclusion
In this answer, we saw that a Phaser extends the latch and barrier ideas with a changing number of parties and numbered phases that can be reused. register() adds a party and arriveAndDeregister() arrives and removes it; arriveAndAwaitAdvance() arrives and waits like a barrier; arrive() signals without waiting, which with awaitAdvance() gives latch-like use; and onAdvance() runs between phases and terminates the phaser when it returns true, which by default happens once no parties remain.
Open with
A Phaser is a reusable barrier where the number of parties can change and the phases are numbered. CountDownLatch is one-shot and CyclicBarrier has a fixed party count, and Phaser removes both limits. register adds a party, from the current phase onwards, and arriveAndDeregister counts as an arrival in this phase and then removes the party, so later phases need one fewer. arriveAndAwaitAdvance is the barrier operation: arrive and block until every registered party has arrived, then the phase number goes up and everyone is released. arrive on its own signals without waiting, so a worker can say it's done and carry on, and a coordinator that isn't a party can wait with awaitAdvance on the phase number, which gives you a latch you can reuse.
Then, if they want more
Between phases, onAdvance runs once in the last arriving thread, and if it returns true the phaser terminates. The default returns true when no parties are left, so when the last party deregisters the phaser shuts down. After termination, every wait returns immediately with a negative phase.
Two differences from CyclicBarrier worth mentioning: arriveAndAwaitAdvance isn't interruptible, and a Phaser has no broken state, so one party failing doesn't release the others.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does Phaser offer beyond CountDownLatch and CyclicBarrier, and how do register...": https://scrivio-interview-library.netlify.app/java/synchronizers/c9d1b714e1/