Structured concurrency
Manage concurrent task lifetimes as a single unit of work.
Code Comparison
✕ Java 8
ExecutorService exec =
Executors.newFixedThreadPool(2);
Future<User> u = exec.submit(this::fetchUser);
Future<Order> o = exec.submit(this::fetchOrder);
try {
return combine(u.get(), o.get());
} finally { exec.shutdown(); }
✓ Java 25 (Preview)
try (var scope = new StructuredTaskScope
.ShutdownOnFailure()) {
var u = scope.fork(this::fetchUser);
var o = scope.fork(this::fetchOrder);
scope.join().throwIfFailed();
return combine(u.get(), o.get());
}
Why the modern way wins
No thread leaks
All forked tasks complete before the scope closes.
Fast failure
ShutdownOnFailure cancels siblings if one fails.
Clear structure
Task lifetime matches the lexical scope in code.
Old Approach
Manual Thread Lifecycle
Modern Approach
StructuredTaskScope
Since JDK
25
Difficulty
advanced
JDK Support
Structured concurrency
Available
Preview in JDK 25 (fifth preview, JEP 505). Requires --enable-preview.
How it works
Structured concurrency treats a group of concurrent tasks as one operation. If any subtask fails, the others are cancelled. The scope ensures no thread leaks and gives clear parent-child relationships.