☕ 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.
More Java Questions
View all →- Intermediate What is the Java Collections Framework?
- Intermediate What is the difference between ArrayList and LinkedList?
- Intermediate What is HashMap in Java and how does it work internally?
- Intermediate What is the difference between HashMap and HashTable in Java?
- Intermediate What is the difference between HashMap and LinkedHashMap?