Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
When should you use CountDownLatch instead of CyclicBarrier?
| CountDownLatch | CyclicBarrier |
| One-time use; count can only decrease and never resets. | Reusable; automatically resets once the barrier trips. |
| Some threads call countDown() while typically different threads call await(). | All participating threads call await() themselves, waiting for each other. |
| Good for "wait until N independent events have happened." | Good for "wait until N threads all reach the same point, then let them all proceed together." |
| No barrier action. | Supports an optional Runnable barrier action run once all parties arrive. |
Use CountDownLatch when the events counting down aren't necessarily symmetric, for example a main thread waiting for several independent worker services to finish initializing before starting the application.
Use CyclicBarrier when a fixed group of worker threads needs to repeatedly synchronize at a shared checkpoint across multiple phases of computation, since it can be reused after each phase completes.
More Related questions...