J.VI · Java 25 practice question
A web framework binds a request context with ScopedValue and invokes user code inside a run call. How does data flow between the framework's entry method and deeper methods invoked during that run call?
Check answer and explanation
Correct answer: Data is shared in one direction from the caller of run to downstream methods for the duration of that run call.
Explanation
ScopedValue's run method shares data one way from the binding caller to methods invoked inside run, and the binding lasts only for the lifetime of that run call.
Common trap
Allowing user code to overwrite the value for the caller is tempting because ThreadLocal permits two-way mutation via set(), whereas ScopedValue enforces one-way sharing. Keeping the binding alive until explicitly cleared is also a ThreadLocal habit that ScopedValue eliminates by scoping the binding strictly to run.
Source: JEP 506: Scoped Values — Description
Checked against the cited text on 2026-11-25.