The Structured Concurrency Model
Treat related tasks as a unit.
The Structured Concurrency Model is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Structured Concurrency
Structured concurrency (JEP 453, preview) treats a group of related concurrent tasks as a single unit of work.
If the parent task splits into subtasks, those subtasks must all complete before the parent moves on, much like a block scope in ordinary code.
The Problem It Solves
With raw executors, a spawned task can outlive its creator, leak, or be forgotten. Errors in one branch may go unnoticed while others keep running.
This leads to thread leaks, orphaned work, and confusing failure handling.
Tasks as a Tree
Structured concurrency enforces a parent-child relationship. Subtasks form a tree rooted at the task that started them.
- A subtask cannot outlive its parent scope
- The parent waits for all children to finish
- Cancellation flows down the tree
Mirroring Sequential Code
The big idea is that concurrent code should read like sequential code. A scope opens, work forks, and the scope closes only after everything joins.
This makes lifetimes obvious and errors easy to reason about.
The Old Unstructured Way
Here is a familiar but unstructured approach using a plain executor. It works, but nothing guarantees the two futures share a coordinated lifetime.
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) throws Exception {
try (ExecutorService exec = Executors.newVirtualThreadPerTaskExecutor()) {
Future<String> user = exec.submit(() -> "Ada");
Future<Integer> order = exec.submit(() -> 7);
System.out.println(user.get() + " has " + order.get() + " orders");
}
}
}Why Coordination Matters
In the previous example, if fetching the user fails, the order task keeps running uselessly. You must manually cancel it and handle exceptions from each get().
Structured concurrency automates this coordination.
Preview Status
Structured concurrency ships as a preview API in the java.util.concurrent package. To use it you compile and run with --enable-preview.
The central class is StructuredTaskScope, covered in the next lesson.
Lifetime Guarantees
Within a structured scope:
- All subtasks complete, fail, or are cancelled before the scope exits
- No subtask escapes into the background
- The call stack reflects the real task hierarchy in thread dumps
Observability Benefit
Because the subtask tree is explicit, JDK tooling can show it. A thread dump groups subtasks under their parent scope, making diagnosis far clearer than scattered pool threads.
Pairs Well with Virtual Threads
Each subtask in a structured scope runs on its own virtual thread by default. The cheapness of virtual threads is what makes fanning out into many subtasks practical.
The two features were designed to work hand in hand.
Mental Model
Think of a structured scope like a try-with-resources block for concurrency: you open it, fork work, join, then close. The closing brace is your guarantee that nothing is left running.
Quick Check
Test your grasp of the core principle.
Recap
You met the structured concurrency model:
- Related tasks form a tree bound to a scope's lifetime
- Concurrent code reads like sequential code
- It is a preview API requiring
--enable-preview - It pairs naturally with virtual threads
Next: the StructuredTaskScope API itself.
Frequently asked questions
Is the “The Structured Concurrency Model” lesson free?
Yes — the full text of “The Structured Concurrency Model” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Structured Concurrency Model”?
Treat related tasks as a unit. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Structured Concurrency Model” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Structured Concurrency Model
- StructuredTaskScope
- Shutdown on Failure and Success
- Error Handling and Cancellation