Sub topic 13
Sharing bindings with forked subtasks
This chapter assumes you know ScopedValue.where(...).run(...), get, and the thread behavior from the previous chapter. There, a plain new Thread did not see the binding. Structured concurrency is the supported way to share a binding with other threads.
The problem: new threads start unbound
A scoped value is bound for the dynamic extent of run. A thread created with new Thread or submitted to an executor can outlive that extent, so it does not inherit the binding. Inheriting would let a thread read a value after its scope had ended.
Subtasks that cannot outlive the scope
StructuredTaskScope ties the lifetime of forked subtasks to a block. The scope's join waits for all subtasks, and the try block cannot exit before then. Every subtask therefore finishes inside the scope that was open when it was forked. That makes it safe to hand them the current bindings.
Subtasks forked in the scope inherit the bindings that were active when the scope was opened. They need no parameter and no copying.
import java.util.concurrent.StructuredTaskScope;
public class Forked {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice").run(() -> {
try (var scope = StructuredTaskScope.open()) {
var profile = scope.fork(() -> "profile for " + USER.get());
var orders = scope.fork(() -> "orders for " + USER.get());
scope.join();
System.out.println(profile.get());
System.out.println(orders.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
}
}Both subtasks run on their own threads, and each reads alice. Compile and run it on JDK 25 with preview features enabled, because StructuredTaskScope is still a preview API there:
// javac --release 25 --enable-preview Forked.java
// java --enable-preview ForkedRebinding stays local
A subtask can open its own nested where(...).run(...). That rebinding applies only inside that subtask. The parent thread and sibling subtasks still see their original value, which follows the same nesting rules as on a single thread.
What is not inherited
- Threads created with
new Thread, or tasks sent to aThreadPoolExecutor, do not inherit bindings, even when they start inside the scope. - Bindings are inherited only by subtasks forked from the scope, and only the bindings active when the scope was opened. A binding you add after opening the scope is not seen by its subtasks.
Summary
Use StructuredTaskScope when work must run in parallel and still see the caller's context. The scope bounds subtask lifetime, so inheriting the binding is safe.
Exercise
A request handler binds USER to "alice" with ScopedValue.where(USER, "alice").run(...). Inside, it forks one subtask with StructuredTaskScope and submits another task to a shared ExecutorService. Which of the two can read USER, and why?
Show answer
The forked subtask can read "alice". StructuredTaskScope guarantees that the subtask finishes before the scope exits, so it can safely inherit the bindings active when the scope was opened. The task on the shared ExecutorService runs on a pooled thread that may outlive the binding. It does not inherit it, so USER is unbound there. Calling USER.get() in it throws NoSuchElementException.