Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

When would you use CountDownLatch rather than Thread.join, ExecutorService.invokeAll or CompletableFuture.allOf to wait for work?

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.

Ways to wait for concurrent work
ConstructWaits forGives backGood fit
Thread.joina thread to terminatenothinga thread we created ourselves
invokeAllall submitted tasks to finishList<Future<T>> with results or errorsa batch of tasks on an executor
CompletableFuture.allOfall futures to completeCompletableFuture<Void>; read each futureasync composition without blocking
CountDownLatcha count to reach zeronothing; true or false from timed awaitevents 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.

Java
Thread t = new Thread(() -> loadCache());
t.start();
t.join(); // fine: the thread ends when loadCache returns
pool.submit(() -> loadCache()); // a pool thread runs it, then waits for more work
// there is no thread to join: the pool thread never ends on its own

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.

Java
List<Callable<Report>> jobs = regions.stream().map(r -> (Callable<Report>) () -> build(r)).toList();
List<Future<Report>> done = pool.invokeAll(jobs, 30, TimeUnit.SECONDS); // unfinished ones are cancelled
for (Future<Report> f : done) {
try { use(f.get()); } // the result, already available
catch (ExecutionException e) { log.warn("region failed", e.getCause()); }
catch (CancellationException e) { log.warn("region timed out"); }
}
CompletableFuture<Void> all = CompletableFuture.allOf(a, b, c);
all.thenRun(() -> combine(a.join(), b.join(), c.join())); // results come from the futures

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.

Java
CountDownLatch startGate = new CountDownLatch(1);
CountDownLatch finished = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
pool.submit(() -> {
try {
startGate.await(); // all 10 wait here
hitEndpoint();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
finished.countDown();
}
});
}
long t0 = System.nanoTime();
startGate.countDown(); // releases all 10 at once
boolean ok = finished.await(30, TimeUnit.SECONDS); // true when all 10 are done

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.

What to say in the interviewAbout 90 seconds aloud

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.
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/