What is CountDownLatch in Java?
Why Interviewers Ask This
Mid-level Java roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.
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.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Java codebase.
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?