← Lessons

quiz vs the machine

Gold1400

Concurrency

The Phaser and Countdown Latch

Two coordination tools, one a one shot gate and one reusable.

5 min read · core · beat Gold to climb

The Phaser and Countdown Latch

Coordinating threads often means making some wait until others reach a point. Two related tools handle this: the countdown latch for a single rendezvous and the phaser for repeated, evolving ones.

A countdown latch starts with a count. Threads call a method that decrements it, and any thread that waits on the latch blocks until the count reaches zero, then proceeds. It is a one shot gate. Once it opens it stays open and cannot be reset, which makes it perfect for a startup barrier where workers wait until initialization finishes.

  • Countdown latch One time, count down to zero then release all waiters.
  • Phaser Reusable across many rounds, advancing in phases.
  • Dynamic parties A phaser lets participants register and deregister between phases.

A phaser is a flexible barrier that works in rounds called phases. All registered parties arrive at the barrier, and when the last one arrives the phase advances and everyone proceeds together to the next round. Unlike a latch, it reuses itself for many phases, which suits iterative algorithms that synchronize at the end of each step.

The phaser also allows the number of participants to change over time. Threads can join or leave between phases, so an algorithm that spawns or retires workers can still coordinate cleanly, something a fixed count latch cannot do.

Key idea

A countdown latch is a one shot gate that opens when its count hits zero, while a phaser is a reusable barrier that advances in phases and allows participants to change.

Check yourself

Answer to earn rating on the learn ladder.

1. How many times can a countdown latch be used?

2. What can a phaser do that a countdown latch cannot?

3. Which tool best fits a startup barrier where workers wait for initialization?