Summary
Introduce scoped values, which enable the sharing of immutable data within and across threads. They are preferred to thread-local variables, especially when using large numbers of virtual threads. This is an incubating API.
Problem
The Motivation section in the JEP explains some of the problems with the design of thread-local variables. These include unconstrained mutability, unbounded persistence, and expensive inheritance. These problems become more pressing in the context of virtual threads (JEP 425).
Solution
ScopedValue that is a value that is set once and is then available for reading for a bounded period of execution by a thread. The API is in an incubator module, the module and package name is jdk.incubator.concurrent
so it is co-located with StructuredTaskScope
(JEP 428). The new classes are:
jdk.incubator.concurrent.ScopedValue
jdk.incubator.concurrent.ScopedValue.Carrier
The javadoc for the existing jdk.incubator.concurrent.StructuredTaskScope
is updated to specify the inheritance of scoped value into child threads.
Specification
Attached is the javadoc for the jdk.incubator.concurrent
module, and specdiffs that show the changes from JDK 19.
- csr of
-
JDK-8286666 Implementation of JEP 429: Scoped Values (Incubator)
- Resolved