Quick revision
The short answer
Thread.join waits for a thread to end, which does not fit pooled threads that keep running after their work. ExecutorService.invokeAll and CompletableFuture.allOf wait for tasks to complete and hand us their results or errors directly, so they are the right choice when we wait for task completion. A CountDownLatch fits waiting for events that are not the end of a task, such as N services reporting ready or a start gate that releases many threads at once. A latch carries no results or exceptions, so errors need separate handling, and when results matter we should prefer the construct that carries them.
What to remember
In this answer, we saw that Thread.join waits for a thread to terminate, which does not fit pooled threads that outlive their tasks. invokeAll and allOf wait for task completion and give us results and errors directly, so they are the right choice when we wait for tasks. A CountDownLatch waits for any kind of event, which makes it the tool for readiness signals and start gates, but it carries no results or exceptions, so it needs separate error handling, and when results matter we prefer the constructs that carry them.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Thread.join waits for a thread to end, which does not fit pooled threads that keep running after their work. ExecutorService.invokeAll and CompletableFuture.allOf wait for tasks to complete and hand us their results or errors directly, so they are the right choice when we wait for task completion. A CountDownLatch fits waiting for events that are not the end of a task, such as N services reporting ready or a start gate that releases many threads at once. A latch carries no results or exceptions, so errors need separate handling, and when results matter we should prefer the construct that carries them.
Why this question is asked
Java offers several ways to wait for concurrent work, and picking the wrong one leads either to awkward code or to hidden bugs, such as a test that hangs because a latch never opens. Interviewers ask this to see whether we understand what each construct actually waits for, rather than treating them as interchangeable. In this answer, we will look at what each one waits for, see where Thread.join stops fitting, compare invokeAll and allOf, and then look at the cases where only a latch expresses what we mean, along with its main weakness.
1What Each Construct Waits For
The key question is what event we are waiting for. Thread.join waits for a specific thread to terminate. invokeAll waits for a list of tasks submitted to an executor to complete. allOf waits for a set of futures to complete. A CountDownLatch waits for a count to reach zero, and what each count stands for is entirely up to us: it can be the end of a task, but also a service finishing startup, a message arriving, or a thread reaching a certain line.
| Construct | Waits for | Gives back | Good fit |
|---|---|---|---|
Thread.join | a thread to terminate | nothing | a thread we created ourselves |
invokeAll | all submitted tasks to finish | List<Future<T>> with results or errors | a batch of tasks on an executor |
CompletableFuture.allOf | all futures to complete | CompletableFuture<Void>; read each future | async composition without blocking |
CountDownLatch | a count to reach zero | nothing; true or false from timed await | events that are not task completion |
Notice that only the latch is free of any link to tasks or threads. That is its strength, because it can represent any event, and its weakness, because it knows nothing about results or failures.
2Where Thread.join Stops Fitting
Thread.join works when we create a thread for one job and the thread ends when the job ends. Modern code rarely works that way. Work runs on pooled threads, from an ExecutorService or a framework, and those threads do not end when our task finishes; they go back to the pool and wait for the next task. Joining a pool thread would wait for the pool to shut down, which is not what we want. And if we want threads to keep running after reaching some point, such as finishing setup, join cannot express that at all, because it only knows about termination.
3InvokeAll and allOf: Waiting for Tasks With Results
When what we wait for really is the completion of tasks, invokeAll and allOf are better than a latch because they also hand us the outcome. invokeAll submits a collection of Callables, blocks until all have finished, and returns their futures, each holding a result or an exception. allOf does the same for futures without blocking; we attach the next step and read each future's value.
Notice that errors arrive with the results: an ExecutionException from get(), or an exceptionally completed allOf. With a latch, the same code would need an extra list for results and another for exceptions, which is exactly what these constructs already provide.
4Where a Latch Is the Right Tool
A latch fits when the event we wait for is not the end of a task. A good example is waiting until N components report ready. Each component starts its own long-running threads, such as a consumer loop or a server, and counts down when it is ready to serve; its threads then keep running. Another example is a start gate: we want many threads to begin at the same moment, for example in a load test, so each waits on a latch with count 1, and the main thread counts it down once to release them all.
Notice that the start gate cannot be expressed with join, invokeAll or allOf at all: none of them can make threads wait for a signal. That is the latch's real niche.
5The Weakness: No Results, No Exceptions
A latch only counts. If a worker fails, the latch does not know; it either opens anyway, when countDown is in finally, or never opens, when it is not. Results must be collected separately in a thread-safe structure, and failures must be recorded separately and checked after await. This is easy to forget, and it is the root of many hanging or silently wrong tests.
So the rule of thumb is: if we are waiting for tasks and care about their results or failures, use invokeAll in blocking code or allOf in async code, or on Java 21 and later simply run blocking tasks on virtual threads and call get on each future. Reach for a latch when the event is something other than task completion, and then add explicit error handling, such as a failures queue, alongside it.
6Follow-up questions
How does CyclicBarrier differ from CountDownLatch?
A CyclicBarrier makes a fixed number of threads wait for each other at a point, and then it resets for the next round, so it can be reused. A latch is one-shot and the threads that count down do not have to wait. A barrier fits phased algorithms where all threads must finish step 1 before any starts step 2.
Can a latch tell me which worker did not count down?
No, it only knows the remaining count. If we need to know which component is missing, we track readiness per component, for example in a ConcurrentHashMap of names to status, and use the latch only for the waiting, or use a structure that waits on named futures.
Is invokeAll still useful with CompletableFuture available?
Yes, in blocking code it is the simplest way to run a batch and get all results with a single overall timeout, and with a virtual-thread executor it scales well. allOf fits better when the surrounding code is already composing futures without blocking.
7Conclusion
In this answer, we saw that Thread.join waits for a thread to terminate, which does not fit pooled threads that outlive their tasks. invokeAll and allOf wait for task completion and give us results and errors directly, so they are the right choice when we wait for tasks. A CountDownLatch waits for any kind of event, which makes it the tool for readiness signals and start gates, but it carries no results or exceptions, so it needs separate error handling, and when results matter we prefer the constructs that carry them.
Open with
I choose based on what event I am waiting for. Thread.join waits for a thread to terminate, which works only when I created a thread for one job.
Then, if they want more
With pooled threads it does not fit, because the thread goes back to the pool and never ends on its own.
If I am waiting for tasks to finish, I prefer invokeAll in blocking code or CompletableFuture.allOf in async code, because they also carry the outcome: invokeAll returns futures with each result or exception and can take an overall timeout, and with allOf I read the values from the futures and failures show up as an exceptionally completed future.
A CountDownLatch is for events that are not task completion. Two typical cases: waiting until N components report ready, where each counts down once it is ready but keeps running, and a start gate, where many threads wait on a latch with count one and the main thread releases them all at once, for example in a load test.
None of the other constructs can express that. The catch is that a latch only counts. It carries no results or exceptions, so I must collect results separately and record failures myself, and use countDown in finally with a timed await. So when results matter, I use the construct that carries them.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "When would you use CountDownLatch rather than Thread.join, ExecutorService.invokeAll or...": https://scrivio-interview-library.netlify.app/java/synchronizers/f5d95303cb/