Scrivio Interview library
ConceptPrincipal6 min read · updated 12 November 2026

CompletableFuture got timeouts only in Java 9 (orTimeout, completeOnTimeout), and cancelling it never stops the underlying work. What does this show about its design, how does structured concurrency address it, and what does that mean for multi-year bets?

CompletableFuture models a result, not the computation that produces it, so it has no handle on that work and cannot stop it. Its timeouts and cancellation therefore only affect the callers who are waiting, while the work carries on and keeps using threads and connections. Structured concurrency links the two: subtasks are forked inside a scope and are interrupted when the scope fails, times out or is cancelled. It is still a preview in Java 25, so for multi-year bets we should prefer designs where cancellation reaches the work, such as deadlines passed to clients, and check its status before planning around it.

Background to this question

Late additions to an API often reveal its original model. When timeouts arrive four years after the class, and cancellation never quite works, that is a sign the class was built around a different idea than the one people use it for. Understanding this helps senior engineers choose which abstractions to build on for years. In this answer, we will look at what CompletableFuture actually models, see why timeouts and cancellation stop only the waiting, look at how structured concurrency connects result and work, and turn that into guidance for long-term technical bets.

1What CompletableFuture Actually Models

A CompletableFuture is a container for a value that will be set later, together with a list of callbacks to run once it is set. Anyone holding it may set the value with complete. It does not know who will do that: a supplyAsync task, an HTTP client callback, a message listener, or a test. That is the strength of the design. Any asynchronous source can be adapted to it, and many independent pieces of code can react to the same result.

But it also means the future has no reference to a thread, a task or a request. Even supplyAsync creates the future and a separate task that completes it; the future does not keep a link back. So the class can describe what the result will be, but it cannot do anything to the computation.

2Why Timeouts and Cancellation Only Affect the Waiters

Once we see the model, the behaviour of timeouts and cancellation follows. orTimeout, added in Java 9, completes the future with a TimeoutException when time is up. cancel completes it with a CancellationException, and its Javadoc says the mayInterruptIfRunning flag has no effect. Both are just ways of completing the container early. Callers stop waiting and dependent stages see the failure, but the code that was producing the value is untouched.

Java
CompletableFuture<Report> f = CompletableFuture.supplyAsync(this::buildReport, pool) // 30 s of work
.orTimeout(2, TimeUnit.SECONDS);
f.join(); // TimeoutException after 2 s
f.cancel(true); // false: already completed by the timeout
// buildReport keeps running for 28 more seconds on a pool thread
// its result is discarded when it tries to complete the future

Notice the cost under load. If every request times out after 2 seconds but its work runs 30, each request leaves a pool thread busy for 28 seconds after the user got an error. The service looks as if it handled the request, while its threads quietly fill up with orphaned work. The late arrival of timeouts in Java 9 is a hint too: in Java 8 the design focused on composing results, and limiting the work behind them was never in its scope.

3How Structured Concurrency Links Result and Work

Structured concurrency starts from the opposite idea: the unit is the task, and tasks are organised like a block of code. We open a scope, fork subtasks inside it, each usually on its own virtual thread, and join the scope before leaving the block. The scope owns its subtasks' threads. When one subtask fails under the default policy, or the scope times out, or the thread that owns the scope is interrupted, the scope cancels the remaining subtasks by interrupting their threads. When the block ends, no subtask can still be running.

Java
// Java 25 preview API (--enable-preview); names have changed between previews, check your JDK
try (var scope = StructuredTaskScope.open(
StructuredTaskScope.Joiner.<Object>awaitAllSuccessfulOrThrow(),
cfg -> cfg.withTimeout(Duration.ofSeconds(2)))) {
var user = scope.fork(() -> users.find(id)); // blocking call on a virtual thread
var orders = scope.fork(() -> orders.recent(id));
scope.join(); // fails fast; on timeout or failure,
// the other subtask's thread is interrupted
return new Page(user.get(), orders.get());
} // no subtask outlives this block
Result holder versus scoped tasks
QuestionCompletableFutureStructured concurrency
What is modelleda result anyone may completesubtasks owned by a scope
Timeout reaches the workno, only the waitersyes, threads are interrupted
Cancel on failuremanual, and it only stops waitingbuilt into the joiner policy
Lifetime of workcan outlive the callerends with the scope block
Status in Java 25final since Java 8preview, check your JDK

Notice what has changed. The timeout and the failure now reach the threads doing the work, through interruption. Blocking JDK calls such as socket reads on virtual threads respond to it, so the work actually stops. The result and the computation are tied together by the scope, which is exactly what CompletableFuture lacks.

4What This Means for Multi-Year Bets

The first lesson is to prefer designs where cancellation reaches the work, whatever API we use today. The most portable form is a deadline passed to the thing doing the work: HTTP clients get connect and read timeouts from the remaining budget, databases get query timeouts, and downstream services receive the remaining time in a header so they can stop early. These work with CompletableFuture, with blocking code on virtual threads, and with structured concurrency, so they do not depend on any one bet.

The second lesson is about timing. Structured concurrency is the clearest fix for the model problem, but in Java 25 it is still a preview feature, and its API has changed between previews. Building a platform on it now means accepting --enable-preview in production and rewriting when the final API lands. For most organisations the better plan is to write fan-out code as plain blocking calls on virtual threads behind a small internal helper. When structured concurrency becomes final in the JDK we run, we change the helper, not hundreds of call sites.

The third lesson is general. When an API's model does not match how we use it, the gaps show up as features added late and flags that do nothing. Noticing that mismatch early is a good reason to keep such APIs at the edges of our code, behind thin abstractions, instead of letting them shape our business logic.

Step throughMaking cancellation reach the work
  1. Edge deadlineset once per request, passed downstream as remaining time
  2. Fan-out helpervirtual threads now, structured concurrency when final
  3. Client timeoutsconnect, read and query timeouts from the remaining time
  4. Downstream servicesread the remaining time and stop early

4 layers, from the top down. Step through them one at a time.

5Follow-up questions

Could CompletableFuture be changed to interrupt the producing thread?

Not safely. A future can be completed by any code, including shared I/O threads that serve many requests. Interrupting whatever thread happened to be running a stage could break unrelated work. The class would need a different model, with an owner for the work, which is what structured concurrency provides.

Is it reasonable to use structured concurrency in production on Java 25?

Only with care. It needs --enable-preview, the API may change in the next release, and libraries may not support it. A team that controls its JDK upgrades and isolates the usage behind a helper can try it; a shared platform usually should wait until it is final.

Do virtual threads alone solve the cancellation problem?

Partly. Plain blocking code on a virtual thread can be interrupted, and blocking calls respond to it, so a request thread that is interrupted stops its work. What virtual threads alone do not give us is the scope that interrupts sibling subtasks automatically when one fails; that is what structured concurrency adds.

6Conclusion

In this answer, we saw that CompletableFuture models a result rather than the computation, so timeouts and cancellation only complete the container early and leave the work running and using resources. Structured concurrency ties subtasks to a scope and interrupts them on failure, timeout or cancellation, linking result and work, but it is still a preview in Java 25. For multi-year bets, deadlines passed to clients and downstream services make cancellation reach the work with any API, and a thin fan-out helper lets us adopt structured concurrency once it is final.

What to say in the interviewAbout 90 seconds aloud

Open with

What it shows is that CompletableFuture models a result, not a computation. It is a container plus callbacks that anyone can complete, and it has no reference to the thread or request producing the value.

Then, if they want more

That is why it can adapt any async source, but also why cancel ignores the interrupt flag and why orTimeout, added in Java 9, only completes the container early. In both cases the callers stop waiting, while the work keeps running and holds threads and connections, and under load that orphaned work piles up.

Structured concurrency takes the opposite view: subtasks are forked inside a scope that owns their threads, and when one fails, the scope times out or is cancelled, the others are interrupted, and nothing outlives the block. So the result and the work are linked.

But in Java 25 it is still a preview, and its API has changed between previews, so I would check its status before planning around it.

For multi-year bets I would prefer designs where cancellation reaches the work regardless of API: a deadline at the edge, client connect, read and query timeouts derived from the remaining time, and the remaining budget passed downstream.

And I would put fan-out behind a thin internal helper on virtual threads, so we can switch it to structured concurrency later without touching business code.

About this answer

Last updated
12 November 2026
Versions it names
Java 25, Java 9, Java 8
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 "CompletableFuture got timeouts only in Java 9 (orTimeout, completeOnTimeout), and...": https://scrivio-interview-library.netlify.app/java/completable-future/5c99c67963/