CountDownLatch vs CyclicBarrier vs Semaphore
Fast recall
Quick revision
Core answer
Ability to match the right coordination primitive to a problem rather than always reaching for synchronized.
Confusing CountDownLatch (cannot be reset) with CyclicBarrier (can be reset and reused), leading to incorrect designs for iterative parallel algorithms.
Note that Phaser (Java 7+) generalises both CountDownLatch and CyclicBarrier with dynamic party registration and multiple phases.
Interview-ready guide
Interview answer
CountDownLatch, CyclicBarrier, and Semaphore solve three different coordination problems — one-time gates, reusable meeting points, and permit pools.
CountDownLatch:
- One-shot gate: count down from N; when zero, all waiting threads proceed; cannot be reset
- Use for: waiting until N services initialize, waiting for N subtasks to complete
CyclicBarrier:
- Reusable meeting point: N threads call
await(), all block until everyone arrives, all proceed together, barrier resets automatically - Use for: phased computations where all threads must finish step K before starting step K+1
- If one thread fails: barrier breaks, all others get
BrokenBarrierException
Semaphore:
- Permit pool:
acquire()takes a permit (blocks if none available),release()returns one - Any thread can release — not just the acquirer (unlike a mutex)
- Use for: rate limiting, connection pooling
Semaphore(1)is not a true mutex — lacks reentrancy; a thread acquiring twice deadlocks itself
I recommend Latch for one-time initialization waits, Barrier for iterative parallel algorithms, and Semaphore for resource access limiting.
CountDownLatch vs CyclicBarrier vs Semaphore
Direct Answer
CountDownLatch is a one-shot countdown that lets one or more threads wait until N operations complete. CyclicBarrier is a reusable rendezvous point where N threads all wait for each other before any proceeds. Semaphore controls access to a pool of N permits, making it suitable for rate-limiting or bounded resource pools.
Common Mistakes & Pitfalls
Confusing CountDownLatch (cannot be reset) with CyclicBarrier (can be reset and reused), leading to incorrect designs for iterative parallel algorithms.
Check your understanding
Answer these without looking back at the article.
- How does Phaser differ from CountDownLatch and CyclicBarrier?
- What happens to a CyclicBarrier when one thread is interrupted while waiting?
- How would you implement a rate limiter using Semaphore?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.