Quick revision
The short answer
Structured concurrency ties the subtasks a method starts to a scope that the method owns: every subtask must finish before the scope closes, a failure can cancel the siblings automatically, and thread dumps show which task started which. With a plain ExecutorService, submitted tasks are independent of the code that started them, so a failure in one leaves the others running unless we cancel them by hand. In Java 25, StructuredTaskScope is still a preview API (its fifth preview), and its shape has changed between previews, so check the status in your JDK.
What to remember
In this answer, we saw that an executor does not tie submitted tasks to the code that submitted them, so a failure in one subtask leaves the others running unless we cancel them ourselves. StructuredTaskScope gives fan-out the same shape as a method call: subtasks are forked inside a try-with-resources block, join() waits for them according to a policy, a failure cancels the siblings by interrupting their virtual threads, and thread dumps show the parent-child tree. In Java 25 it is still a preview API that needs --enable-preview, and its API has changed between previews.
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.
Structured concurrency ties the subtasks a method starts to a scope that the method owns: every subtask must finish before the scope closes, a failure can cancel the siblings automatically, and thread dumps show which task started which. With a plain ExecutorService, submitted tasks are independent of the code that started them, so a failure in one leaves the others running unless we cancel them by hand. In Java 25, StructuredTaskScope is still a preview API (its fifth preview), and its shape has changed between previews, so check the status in your JDK.
Why this question is asked
Fan-out code is everywhere in services: to build one response we load a user, their orders and their recommendations at the same time and combine the results. It looks simple with an executor, but the hard part is what happens when one of the calls fails or the request is cancelled. Interviewers ask about StructuredTaskScope to see whether we understand that lifetime problem, not just the new syntax, and whether we know that the API is not final yet. In this answer, we will recap fan-out with an executor, reproduce the leak it allows, see how a scope fixes it, look at cancellation and observability, and finish with the API's status in Java 25.
1A Quick Recap of Fan-Out With an ExecutorService
An ExecutorService is a pool of threads that runs the tasks we submit to it. When we call submit(), we get back a Future, a handle we can later ask for the result with get(). The executor does not know or care which method submitted the task. Once submitted, the task belongs to the pool, not to us.
That is fine for background jobs, but in fan-out code the subtasks only make sense as part of the method that started them. If the method gives up, its subtasks are useless work. The executor has no idea of this relationship, so it is up to us to cancel them, and it is easy to forget.
2Reproducing the Leak
Here is a method that loads a user and their orders in parallel. The user lookup fails quickly, while the orders query is slow.
Notice what happens. user.get() throws, so handle() exits, but nothing stops findOrders. It keeps its thread and its database connection until it finishes, and its result is thrown away. Under load, those orphaned tasks pile up and use capacity that real requests need. To fix it by hand we would need a try/finally that cancels every future, and we would need to remember it in every fan-out method.
3How a StructuredTaskScope Ties Subtasks to Their Parent
Structured concurrency applies the rule we already follow for method calls: a call cannot outlive the code that made it. A StructuredTaskScope is opened in a try-with-resources block. We fork subtasks inside it, and each fork runs in its own new thread. Then join() waits for them. When the block ends, close() makes sure that no subtask is still running.
9 messages in order. Play them, or step through them one at a time.
The scope sees the failure, cancels the sibling and only then lets the parent continue.
The default policy of open() waits for all subtasks to succeed and fails as soon as one fails. When findUser throws, the scope cancels findOrders, join() throws a StructuredTaskScope.FailedException that carries the original exception as its cause, and the method exits only after both subtasks are done. So the orphaned work from the previous example cannot happen. Other policies, called joiners in the Java 25 API, let us wait for the first success instead, or collect all results; check your JDK's Javadoc for the exact set.
4Cancellation Interrupts the Subtask Threads
Cancellation in a scope uses the normal Java mechanism: the scope interrupts the threads of the unfinished subtasks. By default those threads are virtual threads, cheap threads managed by the JVM, so forking one per subtask costs very little. An interrupt is a request, not a kill. Blocking calls such as Thread.sleep, socket reads through the JDK, or BlockingQueue.take react to it by throwing InterruptedException, which ends the subtask. A loop that only computes and never checks Thread.interrupted() will keep running until it finishes, so long CPU-bound subtasks should check the flag now and then.
The same applies when the parent itself is interrupted, for example because the request timed out. The interrupt reaches join(), the scope cancels all its subtasks, and the whole tree stops together. With an executor we would have to pass that cancellation down by hand.
5Seeing the Task Tree in a Thread Dump
Because the scope knows which thread forked which subtask, the JDK can show that relationship. The JSON thread dump groups the threads of each scope under the thread that owns the scope, so we can see that two virtual threads belong to one request instead of finding them scattered among thousands of unrelated threads.
With an executor, a dump shows a pool of worker threads and nothing about who submitted their tasks. That makes it hard to answer a simple question during an incident: which request is this slow query part of?
6Its Status in Java 25
StructuredTaskScope first appeared as an incubator module in Java 19 and has been a preview API since Java 21. Java 25 carries its fifth preview, and the API changed noticeably along the way: earlier previews created scopes with constructors such as new StructuredTaskScope.ShutdownOnFailure(), while Java 25 uses the static open() method and a Joiner to choose the policy. To use it at all we compile and run with --enable-preview. So code written against one preview may not compile on the next JDK, and we should check the JEP for our exact release before relying on it.
7Follow-up questions
What is the difference between a Subtask and a Future?
A Subtask is the handle that fork() returns. Unlike a Future, we are meant to read it only after join() has returned, and its get() does not block; calling it earlier, or on a subtask that failed, throws IllegalStateException. The design pushes us to wait for the whole group through the scope instead of waiting on each subtask separately.
Can I still use StructuredTaskScope with platform threads?
Yes. open() has an overload that takes a configuration function, and through it we can supply a different ThreadFactory, a name for the scope, or a timeout. The default of one virtual thread per subtask is usually right, because the subtasks are typically I/O calls that spend most of their time waiting.
How do scoped values fit with structured concurrency?
A ScopedValue bound in the parent is automatically visible in every subtask forked inside the scope, without copying. That is one reason the two features were designed together: request context such as the current user flows into the subtasks safely, and the binding ends when the parent's call ends.
8Conclusion
In this answer, we saw that an executor does not tie submitted tasks to the code that submitted them, so a failure in one subtask leaves the others running unless we cancel them ourselves. StructuredTaskScope gives fan-out the same shape as a method call: subtasks are forked inside a try-with-resources block, join() waits for them according to a policy, a failure cancels the siblings by interrupting their virtual threads, and thread dumps show the parent-child tree. In Java 25 it is still a preview API that needs --enable-preview, and its API has changed between previews.
Open with
Structured concurrency solves a lifetime problem. When I fan out with an ExecutorService, the tasks I submit are not connected to my method. If one fails, I get an exception from its future and my method exits, but the other tasks keep running, holding threads and connections, unless I remember to cancel them in a finally block.
Then, if they want more
StructuredTaskScope makes the subtasks part of a scope that my method owns. I open the scope in a try-with-resources block, fork the subtasks, and call join. Each subtask runs in its own virtual thread by default.
With the default policy, if one subtask fails, the scope interrupts the others, join throws, and close guarantees nothing is still running when the block ends. If my own thread is interrupted, the cancellation flows down to the whole tree.
There is also an observability benefit: the JSON thread dump from jcmd shows each scope with its owner thread and its forked threads, so I can see which request a thread belongs to.
On status, it is still a preview in Java 25, the fifth one, and the API changed across previews, from constructors like ShutdownOnFailure to the open method with joiners. So it needs --enable-preview and I would check the JEP for the exact JDK before using it in production.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 19, 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
- 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 "What problem does structured concurrency (StructuredTaskScope) solve compared with...": https://scrivio-interview-library.netlify.app/java/virtual-threads/44afc357fa/