Java FAQ: Top Questions
47. What is the difference between CountDownLatch and CyclicBarrier?
CountDownLatch
and
CyclicBarrier
in
java.util.concurrent
are synchronization aids for coordinating threads, but they serve different purposes.
-
CountDownLatch:
- Allows one or more threads to wait until a set of operations (count) completes.
- Non-reusable; count cannot be reset.
- Use case: Waiting for multiple tasks to finish.
-
CyclicBarrier:
- Allows a fixed number of threads to wait for each other at a barrier point.
- Reusable; resets after all threads reach the barrier.
- Use case: Iterative parallel algorithms.