Quick revision
The short answer
With only final APIs, we submit both calls to Executors.newVirtualThreadPerTaskExecutor() inside try-with-resources and return new Result(u.get(), o.get()). If findUser fails quickly, u.get() throws ExecutionException, but the executor's close() at the end of the try block still waits for the slow orders call, and nothing cancels it automatically; we have to call o.cancel(true) ourselves in a catch block. Structured concurrency is designed to cancel the sibling automatically, but it is still a preview API in Java 25, and in either approach both calls should have timeouts.
What to remember
In this answer, we fetched a user and their orders in parallel with Executors.newVirtualThreadPerTaskExecutor() and two futures, which on the happy path takes as long as the slower call. When findUser fails quickly, u.get() throws ExecutionException, but close() still waits for the slow orders call, because nothing cancels it. We fixed that with o.cancel(true) in a catch block and a shared deadline, backed by client timeouts, and saw that structured concurrency makes sibling cancellation automatic, but is still a preview API in Java 25.
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.
With only final APIs, we submit both calls to Executors.newVirtualThreadPerTaskExecutor() inside try-with-resources and return new Result(u.get(), o.get()). If findUser fails quickly, u.get() throws ExecutionException, but the executor's close() at the end of the try block still waits for the slow orders call, and nothing cancels it automatically; we have to call o.cancel(true) ourselves in a catch block. Structured concurrency is designed to cancel the sibling automatically, but it is still a preview API in Java 25, and in either approach both calls should have timeouts.
Why this question is asked
Fetching two independent things in parallel is the most common reason to start threads in a service: a page needs the user and their orders, and doing the calls one after the other wastes the time of the faster one. Virtual threads make this cheap and keep the code blocking and readable. Interviewers ask this question because the happy path is easy, and the interesting behaviour is in the failure path, where the code quietly waits for work whose result it will throw away. In this answer, we will write the straightforward version, trace what happens when the user call fails fast, fix it with explicit cancellation and timeouts, and compare it with what structured concurrency offers.
1The Straightforward Version
We create an executor that starts a virtual thread per task, submit both calls, and wait for both results. Since Java 19, ExecutorService is AutoCloseable, and its close() waits for all submitted tasks to finish, so try-with-resources guarantees no thread outlives the method.
On the happy path this is exactly right. Both calls run at the same time, the method takes as long as the slower call, and blocking in get() is cheap because the waiting thread is itself typically a virtual thread that unmounts.
2What Goes Wrong When the User Call Fails Fast
Now suppose findUser fails after 20 milliseconds, for example because the user service returns an error, while findOrders is slow today and takes 5 seconds. u.get() throws ExecutionException after 20 milliseconds, wrapping the user service's error. We are not going to return a result, so the orders are useless. But look at what happens next: the exception leaves the try block, which calls ex.close(), and close() waits for every submitted task to finish. The orders call is still running, so the method waits the full 5 seconds before the exception finally reaches the caller.
Nothing in this code cancels the orders call. The executor does not know that the two tasks belong together or that one failure makes the other pointless. So a fast failure turns into a slow failure, and the orders service does 5 seconds of work for nothing. Under load, with many such requests, this wastes downstream capacity exactly when something is already going wrong.
7 messages in order. Play them, or step through them one at a time.
The failure was known after 20 ms, but the caller hears about it after 5 s.
3Fixing It With Explicit Cancellation and Timeouts
With final APIs, we must cancel the sibling ourselves. Future.cancel(true) interrupts the thread running the task, and most blocking operations in a virtual thread, such as socket reads, respond to an interrupt by stopping. We also put a deadline on both calls, so a slow call cannot hold the request indefinitely even when nothing fails.
Notice the remaining subtlety: close() still waits for the cancelled task to actually stop. If findOrders ignores interrupts, for example because its HTTP client blocks in a way that does not respond to them, cancellation only marks the future and the thread keeps running until the call returns. That is why the HTTP clients themselves should also have connect and read timeouts; the client timeout is what reliably ends the call. Also notice that this code still waits for the user first: if orders fail first, we only notice after u.get() returns. Handling either-fails-first correctly by hand needs more code, such as completion callbacks, which is exactly what structured concurrency packages up.
4What Structured Concurrency Adds
Structured concurrency treats the two calls as one unit of work with a scope: subtasks are forked inside the scope, the scope waits for them, and if one fails, the scope cancels the others automatically and propagates the failure, whichever subtask fails first. The scope also guarantees that no subtask outlives it and makes the parent-child relationship visible in thread dumps. In Java 25 the API, StructuredTaskScope, is still a preview feature, so it needs --enable-preview and may change between releases, and the question rules it out. For comparison, it looks roughly like this in Java 25; check your version, since the API has changed across previews.
| Behaviour | Executor + Future (final) | StructuredTaskScope (preview) |
|---|---|---|
| Runs calls in parallel | yes | yes |
| Waits for all tasks before returning | yes, close() | yes, end of scope |
| Cancels the sibling on failure | only if we call cancel(true) | automatically |
| Reacts to whichever fails first | needs extra code | yes |
| Timeouts | get(timeout) plus client timeouts | scope configuration plus client timeouts |
The failure behaviour we wrote by hand, cancel the sibling as soon as one fails, is the default here, and timeouts can be configured on the scope. Until the API is final, the executor version with explicit cancellation and client timeouts is the production-safe choice.
5Follow-up questions
Why not use CompletableFuture instead?
CompletableFuture.supplyAsync with a virtual thread executor and thenCombine expresses the happy path neatly, and orTimeout adds a deadline. But cancelling a CompletableFuture does not interrupt the thread running it, so the slow call keeps running anyway. It has the same cancellation gap, in a different form.
Does cancel(true) always stop the orders call?
It interrupts the virtual thread. Blocking socket operations in a virtual thread respond to interruption, and so do most java.util.concurrent waits, but some libraries catch and ignore interrupts. Client-level timeouts are the reliable backstop, which is why both calls should have them.
Why is the deadline shared between the two get() calls?
Because the request has one budget. If each get() had its own 2-second timeout, the worst case would be 4 seconds. Computing the remaining time from one deadline keeps the total at 2 seconds.
6Conclusion
In this answer, we fetched a user and their orders in parallel with Executors.newVirtualThreadPerTaskExecutor() and two futures, which on the happy path takes as long as the slower call. When findUser fails quickly, u.get() throws ExecutionException, but close() still waits for the slow orders call, because nothing cancels it. We fixed that with o.cancel(true) in a catch block and a shared deadline, backed by client timeouts, and saw that structured concurrency makes sibling cancellation automatic, but is still a preview API in Java 25.
Open with
With final APIs, I'd open a newVirtualThreadPerTaskExecutor in try-with-resources, submit findUser and findOrders as two futures, and return a new Result with u.get and o.get. On the happy path that takes as long as the slower call, and since Java 19 the executor's close waits for all tasks, so nothing leaks past the method.
Then, if they want more
The problem is the failure path. If findUser fails after 20 milliseconds while findOrders takes 5 seconds, u.get throws an ExecutionException quickly, but leaving the try block calls close, and close waits for the orders task. So the caller gets the error after 5 seconds, and the orders service did all that work for nothing. Nothing cancels the sibling automatically.
To fix it, I'd catch ExecutionException, TimeoutException and InterruptedException, call cancel true on both futures, which interrupts the orders thread, and rethrow. I'd also give both get calls one shared deadline, and, importantly, put connect and read timeouts on the clients, because cancel only interrupts, and a client that ignores interrupts would still keep close waiting.
Structured concurrency is designed for exactly this: if one subtask fails, the scope cancels the others and propagates the failure. But StructuredTaskScope is still preview in Java 25, so for production I'd use the executor version.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 19
- 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
- JEP 444, Virtual Threads
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 "Using only final APIs, write code that fetches a user and their orders in parallel on...": https://scrivio-interview-library.netlify.app/java/virtual-threads/6b1546e9af/