☕ Java Intermediate

What is CountDownLatch in Java?

Answer

CountDownLatch is a synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. Initialize it with a count: CountDownLatch latch = new CountDownLatch(3). Each worker thread calls latch.countDown() when it finishes (decrements the count). The waiting thread calls latch.await(), which blocks until the count reaches zero. Once the count reaches zero, it cannot be reset — for a reusable latch, use CyclicBarrier. Common use cases: waiting for all services to start before serving requests, waiting for all results before computing a summary, and testing concurrent scenarios.