A few questions on ScopedValue

Alan Bateman Alan.Bateman at oracle.com
Mon Jul 17 07:40:25 UTC 2023


On 16/07/2023 23:30, viraj shetty wrote:
> I have read JEP 446 related to Scoped Values and have been playing 
> with it using Java 21 early release. I have couple of minor 
> questions/clarifications.
>
> 1. The JEP seems to convey the idea that the ScopeValue objects are 
> one way communication down the call chain. But the scoped value can be 
> an object which is mutable and it could be modified deep inside the 
> call stack. Effectively that is two way communication - even though 
> the variable itself cannot be modified. Not a big deal but maybe a 
> clarification is useful

It's not too different to method parameters. Call a method with a 
mutable object as a method parameter and the callee may modify it. If 
you think of a ScopedValue object as an implicit method parameter, and 
the callee has access to to it, then it can also modify it.


>
> 2. It also conveyed the idea that scoped values are automatically 
> available for Child Threads. But on playing with it, its available 
> only for child threads of StructuredTaskScope, not for threads started 
> with Thread.ofVirtual() or Executorservice.  Is this because its 
> guaranteed that the threads submitted in structuredTaskScope ends 
> before ScopedValue scope ends ? Making it bounded is the idea ?
>
There is effort under way to improve the JEP text and how this feature 
is presented. In the case of inheritance then it will be clear that it's 
for structured contexts only. The API docs [1] try to make this clear. 
There is no equivalent for unstructured use as it would be the 
equivalent of accessing something after it has gone out of scope.

-Alan

[1] 
https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/lang/ScopedValue.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230717/c0b56bd7/attachment-0001.htm>


More information about the loom-dev mailing list