A few questions on ScopedValue

viraj shetty shetty_viraj at yahoo.com
Tue Jul 18 00:39:23 UTC 2023


Thanks Alan.  Thinking about ScopedValue as an implicit parameter is a good insight. Also - the javadoc for the SopedValue does clarify a great deal about using with StructuredTaskScope. Thanks for pointing me to that. 
As I was reading the ScopedValue Javadoc, I had another question. As part of the API note - the Javadoc says the following about Thread locals.
"Inheritance is expensive - the map of thread-locals to values must be copied when creating each child thread." 
I assume this is referring to InheritableThreadLocals and not the regular ThreadLocals. My understanding is that the regular ThreadLocal objects don't get copied over to child threads   
~ Viraj 
    On Monday, July 17, 2023 at 03:40:35 AM EDT, Alan Bateman <alan.bateman at oracle.com> wrote:  
 
  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/20230718/d9ad32ad/attachment.htm>


More information about the loom-dev mailing list