CountDownLatch is a synchronizer in java.util.concurrent that lets one or more threads wait until a fixed number of operations in other threads have finished. It starts with a count, each countDown() call decreases it, and every thread blocked in await() is released when the count reaches zero. The latch is single-use: the count can never be reset.
Typical uses are waiting for worker threads to finish, holding all workers back until a start signal, and waiting for services to initialise before an application accepts traffic.
You create a latch with a positive count. The class has three methods you use daily:
| Method | Behaviour |
|---|---|
countDown() | Decrements the count. When it hits zero, all waiting threads are released. Calls after zero do nothing. |
await() | Blocks until the count is zero or the thread is interrupted. |
await(long, TimeUnit) | Waits at most the given time and returns true if the count reached zero, false on timeout. |
getCount() returns the current count and is mainly useful for diagnostics and tests. Internally the latch uses an AbstractQueuedSynchronizer, so the writes made by a thread before countDown() happen-before the return of a successful await() in another thread. That memory-visibility guarantee is why you can safely read results produced by workers after the latch opens.
The most common pattern is a coordinator that starts N tasks and waits for all of them. The finally block matters: if a task throws and never calls countDown(), the waiting thread blocks forever.
The order of the lines can differ between runs. Using ExecutorService in try-with-resources works from Java 19 onward, where close() waits for submitted tasks. Using a timeout with await is a good habit in production so a lost countDown() cannot hang a thread forever.
A latch with a count of 1 acts as a gate. Workers call await() on it and all proceed the moment the main thread calls countDown(). This is common in concurrency tests and benchmarks where you want threads to start at the same instant.
Here two latches are combined: one gate to start, one counter to detect completion. Note the Thread.currentThread().interrupt() call, which restores the interrupt flag when you catch InterruptedException.
Developers often confuse these synchronizers. The key difference is that a latch is single-use and the counting threads do not have to be the waiting threads.
| Feature | CountDownLatch | CyclicBarrier | Phaser |
|---|---|---|---|
| Reusable | No | Yes, resets after each trip | Yes, multiple phases |
| Who waits | Any threads calling await() | The same parties that arrive | Registered parties |
| Who signals | Any thread calling countDown() | Each party calling await() | Parties call arrive methods |
| Barrier action | No | Optional | Optional via onAdvance |
| Party count | Fixed | Fixed | Dynamic |
Choose CountDownLatch for a one-time event, CyclicBarrier for repeating rounds with a fixed group, and Phaser when the number of participants changes.
countDown() on an exception path, which hangs the waiter. Always use finally.CyclicBarrier or Phaser.InterruptedException instead of restoring the interrupt flag.For simply waiting on a set of tasks, higher-level tools are often cleaner. ExecutorService.invokeAll returns when all tasks complete, and CompletableFuture.allOf combines futures. On recent JDKs, structured concurrency (StructuredTaskScope, a preview API in the Java 21 to 25 releases) is designed to replace much hand-written latch code. A latch is still the right low-level tool for signalling an event between threads, such as "initialisation complete".
No. Once the count reaches zero it stays zero and await() returns immediately. Create a new latch, or use CyclicBarrier or Phaser for repeated use.
Nothing. After the count is zero, further countDown() calls have no effect and do not throw.
Yes. All methods are safe to call from multiple threads, and countDown() happens-before a successful return from await().
Yes. A virtual thread blocked in await() releases its carrier thread, so latches are fine in virtual-thread code.
CountDownLatch lets threads wait for a set number of events. Use a count equal to the number of tasks to wait for completion, or a count of one as a start gate. Always call countDown() in a finally block, prefer await with a timeout, and remember it cannot be reused. For repeated rounds use CyclicBarrier or Phaser, and for plain task joins consider invokeAll or CompletableFuture.allOf.