Concurrency advanced

Structured concurrency

Manage concurrent task lifetimes as a single unit of work.

✕ 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());
}
🛡️

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
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.

Share 𝕏 🦋 in