Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

In a code review, someone wrote cf.thenApply(x -> otherAsync(x).join()) to avoid nested futures. What is wrong with it?

join() inside the stage blocks the thread running the stage until the other future completes, so a thread from the common pool or an I/O thread sits idle. Under load this starves the pool, and if the inner future needs the same pool to run, it can deadlock once every thread is blocked waiting. It also turns failures into CompletionException wrapped inside another stage. thenCompose(x -> otherAsync(x)) gives the same flat result without blocking.

Background to this question

This line is a common workaround when someone meets a nested future for the first time. It compiles, it returns the right type, and in a unit test with one request it works perfectly. The problems appear only under concurrency, which is exactly why reviewers should catch it. In this answer, we will see what join() does to the thread running the stage, show how it starves and then deadlocks a pool, look at how the error wrapping gets more confusing, and replace it with thenCompose.

1What join() Does Inside a Stage

join() waits until a future is complete and returns its value. It is the unchecked cousin of get(): if the future failed, it throws CompletionException instead of ExecutionException. Waiting means the calling thread is parked and can do nothing else.

When we write cf.thenApply(x -> otherAsync(x).join()), the lambda runs on whichever thread completes cf, for example a ForkJoinPool.commonPool() worker if cf came from supplyAsync, or an HTTP client I/O thread if cf came from an async client. That thread starts otherAsync, then stops and waits for it. For the whole duration of the inner call, a thread that was meant to run short tasks is held hostage.

Java
CompletableFuture<Order> order = findUser(42) // completes on a common-pool worker
.thenApply(u -> findLatestOrderAsync(u).join()); // that worker now waits 300 ms
// The type is right: CompletableFuture<Order>
// The cost is hidden: one pool thread blocked per request in flight

Notice that nothing about the type warns us. The result is CompletableFuture<Order>, just as with thenCompose. The difference is only in what the threads are doing.

2Starvation: Wasted Threads Under Load

The common pool has about as many threads as CPU cores minus one, say 7 on an 8-core machine. If each request holds a worker for 300 ms while it waits, those 7 workers can complete only about 23 requests per second. Every other task that uses the common pool, including parallel streams and any other supplyAsync in the JVM, now queues behind blocked threads. If the stage runs on an HTTP client I/O thread instead, the damage is worse: while that thread waits, it reads no responses for other connections, and they start to time out.

3Deadlock: When the Inner Future Needs the Same Pool

The serious case is when the inner future is computed on the same pool that the outer stage is blocking. Here is a small pool of 2 threads to make it visible.

Java
ExecutorService pool = Executors.newFixedThreadPool(2);
CompletableFuture<Integer> outer(int i) {
return CompletableFuture.supplyAsync(() -> i, pool)
.thenApplyAsync(x -> inner(x).join(), pool); // holds a pool thread while waiting
}
CompletableFuture<Integer> inner(int x) {
return CompletableFuture.supplyAsync(() -> x * 10, pool); // needs a free pool thread
}
outer(1); outer(2); // both pool threads are now inside join()
// inner(1) and inner(2) sit in the queue, no thread is free to run them
// both join() calls wait forever: deadlock
Step throughPool threads waiting for work that cannot start
Pool thread 1Pool thread 2Pool queue
submit inner(1)
join() on inner(1): parked
submit inner(2)
join() on inner(2): parked
inner(1), inner(2) wait for a free thread

5 messages in order. Play them, or step through them one at a time.

Every thread waits for a task that needs a thread.

Both pool threads are waiting for inner tasks, and those inner tasks are waiting in the queue for a free pool thread. Nothing can move. With a larger pool it only takes more concurrent requests to reach the same state, which is why such code usually works in testing and freezes under a traffic peak. The common pool partly protects itself with managed blocking for some joins, but we should not rely on it; with ordinary fixed executors there is no such help.

4Error Handling Gets Murkier

If otherAsync fails, join() throws a CompletionException wrapping the real cause. That exception is thrown inside our thenApply lambda, so the outer future completes exceptionally with it. A handler further down the chain now has to unwrap one level to find the real exception, and the stack trace shows the join() call instead of the place where the inner work actually failed. With thenCompose the outer future simply fails with the inner future's cause, without the extra layer of wrapping from our own lambda.

5Fix: Use thenCompose

thenCompose does exactly what the join() was trying to do, but without waiting on a thread. It calls our function, takes the returned future, and completes the outer future when the inner one completes.

Java
CompletableFuture<Order> order = findUser(42)
.thenCompose(u -> findLatestOrderAsync(u)); // no thread waits
order.join(); // Order{id=901}, only the caller decides to block

So the same result type comes back, no pool thread is held while the order is fetched, and the deadlock scenario disappears because no stage waits for another task on the same pool. If we must block at all, the right place is the edge of the program, for example the controller thread that needs the final answer, and with virtual threads that edge block is cheap.

6Follow-up questions

Is join() ever acceptable inside a stage?

Only when the inner future is known to be already complete, for example inside allOf(...).thenApply(v -> a.join() + b.join()), where allOf guarantees that a and b are done. There join() returns immediately and is just a way to read the value.

Would virtual threads make this pattern safe?

If the stage runs on a virtual thread, blocking in join() is much cheaper and does not starve a small pool. But stages often run on threads we do not choose, such as I/O threads or the common pool, so the code still has the problem wherever it runs. thenCompose is correct on every thread, so it remains the better choice.

How do we spot this in review?

Look for join() or get() inside any lambda passed to a then method or to handle and whenComplete. Many teams add a static analysis rule for it. A thread dump with pool threads parked in CompletableFuture.join is the runtime symptom.

7Conclusion

In this answer, we saw that join() inside thenApply blocks whatever thread runs the stage, wasting common-pool or I/O threads and starving everything that shares them. If the inner future needs the same pool, all threads can end up waiting for tasks that cannot start, which is a deadlock. Failures also arrive wrapped in CompletionException from our own lambda. thenCompose gives the same flat CompletableFuture<Order> without blocking any thread, so it is the correct way to chain one async call after another.

What to say in the interviewAbout 80 seconds aloud

Open with

It gets the right type, but it blocks. join inside the lambda parks the thread running the stage until the inner future completes, and we usually do not choose that thread: it is a common-pool worker or even an HTTP client I/O thread.

Then, if they want more

Under load each request holds a thread for the whole inner call, so the pool starves, and other users of that pool, like parallel streams, queue behind it. If it is an I/O thread, other responses stop being read and time out.

The worst case is a deadlock: if the inner future runs on the same pool, all the pool threads can end up inside join waiting for inner tasks that are stuck in the queue because no thread is free.

It also makes error handling harder, because a failure comes out as a CompletionException thrown from our lambda, so downstream handlers have to unwrap it. The correct version is thenCompose with the same lambda without join. It flattens the nested future, completes when the inner one completes, and never holds a thread while waiting.

The only place I accept join inside a stage is when the future is already complete, such as after allOf.

About this answer

Last updated
12 November 2026
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 "In a code review, someone wrote cf.thenApply(x -> otherAsync(x).join()) to avoid nested...": https://scrivio-interview-library.netlify.app/java/completable-future/1b46319da9/