Sub topic 12
What other threads see
This chapter assumes you can read the earlier examples that declare a ScopedValue and bind it with where(...).run(...). It also assumes you know how to start and join a thread. Scoped values were a preview feature from Java 21 through 24, so on those releases you compile and run with --enable-preview. They became final in Java 25.
A binding belongs to one thread's call stack
A scoped value is bound for the duration of a run call, and that binding is tied to the thread that made the call. Code deeper in that thread's call stack can read it. A different thread has its own call stack, so it has no binding unless something sets one up for it.
This differs from InheritableThreadLocal, which copies values into child threads automatically. Scoped values do not copy anything into a plain new Thread.
public class NotInherited {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice").run(() -> {
Thread t = new Thread(() -> System.out.println(USER.isBound())); // false
t.start();
try {
t.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
}
}The program prints false. The new thread started inside the run call, but its own stack has no binding for USER. Calling USER.get() there would throw NoSuchElementException.
Passing the value explicitly
If a plain thread needs the data, read it in the parent and hand it over as an ordinary variable. The value is immutable, so sharing it this way is safe.
public class PassExplicitly {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice").run(() -> {
String user = USER.get();
Thread t = new Thread(() -> System.out.println("Worker sees " + user));
t.start();
try {
t.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
}
}The worker prints Worker sees alice. It reads a captured local variable, not the scoped value. The worker can also outlive the run call, and the captured string stays valid. A scoped-value binding would not.
Subtasks that do inherit
Scoped values do cross threads in one case: forked subtasks of a StructuredTaskScope. The scope's owner thread waits for its subtasks before leaving the block, so the subtasks cannot outlive the binding. Because of that guarantee, the subtasks can safely see the owner's bindings.
Structured concurrency was still a preview API in Java 25. Its method names changed between releases, so check the documentation for your version. The rule stays the same: bindings pass to forked subtasks, not to threads you start yourself.
Summary of who sees what
| Situation | Sees the binding? |
|---|---|
| Code called by `run` on the same thread | Yes |
| A thread created with `new Thread` inside `run` | No |
| A task submitted to an executor | No |
| A subtask forked in a `StructuredTaskScope` | Yes |
Common errors
- Calling
get()in a new thread and gettingNoSuchElementException. Check withisBound()or pass the value in. - Expecting a pool thread to see the caller's binding.
- Assuming the code works on Java 21 to 24 without
--enable-preview. It will not compile.
Exercise
A request handler binds USER with ScopedValue.where(USER, "bob").run(...). Inside, it submits a task to a shared ExecutorService, and the task calls USER.get(). What happens, and how do you fix it?
Show answer
The task runs on a pool thread that has no binding for USER, so get() throws NoSuchElementException. Read USER.get() in the handler before submitting, capture it in a local variable, and use that variable in the task. Alternatively, fork the work in a StructuredTaskScope so the subtask inherits the binding.