A few questions on ScopedValue
Holo The Sage Wolf
holo3146 at gmail.com
Sun Jul 16 22:58:36 UTC 2023
1) passing a mutable object to a ScopedValue does not give you upcall
communication, it let you downcall an object that connects to a different
place. If I would make an analogy: if I drop down from the 3rd floor a
phone down to you, you can use the phone I dropped down to call me, but you
can't throw stuff up to me.
If you consider this an upwards communication then there is literally no
such thing as one direction communication because I can always send you a
file location and than you can send me messages from that file. (Maybe you
can achieve true one direction communication using some kind of closed
world sandboxes that don't expose anything to the outside world, but that
would be useless)
2) yes, ScopedValues guarantee their integrity, outside of their scope they
are not accessible.
If their value was inherited you could do stuff like (exception omitted for
clarity):
ScopedValue.where(SECRET, "o7")
.run(() -> Thread.ofVirtual(() -> {
Thread.sleep(5000);
System.out.println("your secret is: " + SECRET.get());
}));
System.out.println("your secret is lost");
Which would print:
> your secret is lost
> your secret is: o7
On Mon, Jul 17, 2023, 01:30 viraj shetty <shetty_viraj at yahoo.com> 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
>
> 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 ?
>
> Thanks for the phenomenal work.
> ~ Viraj
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230717/6498d425/attachment-0001.htm>
More information about the loom-dev
mailing list