Java scoped values

자바 범위 값

Bind a value to a dynamic execution scope, then restore the prior binding on exit.

···
html
<div class="v"><header><b>SCOPED VALUE</b><span id="state"></span></header><main><div class="scope"><div class="outer" id="outer">REQUEST ID: A7<div class="inner" id="inner">NESTED: B2</div></div></div><div class="readout" id="readout"></div></main><footer>nested binding restores the outer value</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,3.2vmin,17px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.28} .scope{height:71%;display:grid;place-items:center}.outer{width:84%;height:76%;box-sizing:border-box;border:2px solid var(--accent);border-radius:9px;padding:7px;color:var(--accent);font:600 clamp(12px,2.8vmin,16px) var(--font-sans, sans-serif)}.inner{height:52%;margin:7px 8px 0;padding:5px;border:1px solid var(--accent-2);border-radius:6px;color:var(--accent-2);transition:opacity .25s}.readout{text-align:center;color:var(--fg);font:600 clamp(12px,2.8vmin,16px) var(--font-sans, sans-serif)}
js
let n=0;const values=["A7 (outer)","B2 (nested)","A7 (restored)","unbound"];function draw(){document.getElementById("inner").style.opacity=n===1?"1":".28";document.getElementById("outer").style.opacity=n===3?".28":"1";document.getElementById("readout").textContent="read → "+values[n];document.getElementById("state").textContent="scope "+(n+1)+" / 4";n=(n+1)%4}draw();setInterval(draw,1050)

Java 25 ScopedValue lets downstream calls read request context without passing it through every method parameter. A binding lives only for a bounded dynamic scope; a nested binding of the same key temporarily takes precedence.

Structured child tasks can inherit the parent binding. Mutable values still require synchronization, and code outside the scope cannot read the expired binding.

When to use

Use it to pass read-mostly context such as a request ID through downstream calls.

Open as page ↗