intermediateConcurrency Utilities

What is a Phaser, and how does it differ from CyclicBarrier?

Phaser is a more flexible and reusable synchronization barrier that supports dynamic registration of participants, unlike CyclicBarrier which fixes its number of parties up front. Threads can call register() to join and arriveAndDeregister() to leave dynamically at runtime, whereas CyclicBarrier's party count never changes after construction. Phaser also supports partial arrival: a thread can call arriveAndDeregister() to contribute to the current phase without waiting around for the other participants to finish it. Termination behavior can be customized by overriding onAdvance(), returning true from it to terminate the phaser entirely. Phasers can also be arranged in a tiered, tree-like structure for hierarchical coordination across large numbers of threads. Phaser is the better choice whenever threads need to drop in and out between phases or the set of participants isn't known ahead of time, while CyclicBarrier remains simpler for a fixed group of threads synchronizing repeatedly.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to How does CopyOnWriteArrayList work internally, and when is it appropriate to use?← Back to all Java Concurrency & Multithreading questions