<div dir="ltr">For others that might be interested,<div><br></div><div>I have received a suggestion that fits what I need.</div><div><br></div><div>------------------------------------------------------------------</div><div><div dir="auto" style="color:rgb(0,0,0)">I noticed your question on loom-dev, and because I am not registered, I cannot answer there.</div><div dir="auto" style="color:rgb(0,0,0)"><br></div><div dir="auto" style="color:rgb(0,0,0)">I know one API you can use for that: the opentelemetry context API: <a href="https://javadoc.io/doc/io.opentelemetry/opentelemetry-context/latest/io/opentelemetry/context/Context.html" target="_blank">https://javadoc.io/doc/io.opentelemetry/opentelemetry-context/latest/io/opentelemetry/context/Context.html</a></div><div dir="auto" style="color:rgb(0,0,0)">It's a very small standalone jar, and despite it's root to opentelemetry (and despite what the javadoc says), it does not depend on the other opentelemetry APIs, it is a clean isolated small reusable library.</div><div dir="auto" style="color:rgb(0,0,0)"><br></div><div dir="auto" style="color:rgb(0,0,0)">You use it with try-with-resources and it stacks values one on top of the other (even when reusing the same key).</div><div dir="auto" style="color:rgb(0,0,0)"><br></div><div dir="auto" style="color:rgb(0,0,0)">There is an implementation difference with scoped values: when setting a new value it keeps the old array of values intact (immutability) and then does a copy-on-write of the whole array and inserts the new value. See <a href="https://github.com/open-telemetry/opentelemetry-java/blob/main/context/src/main/java/io/opentelemetry/context/ArrayBasedContext.java" target="_blank">https://github.com/open-telemetry/opentelemetry-java/blob/main/context/src/main/java/io/opentelemetry/context/ArrayBasedContext.java</a></div><div dir="auto" style="color:rgb(0,0,0)">Instead, scoped values are chaining the values like a tree (immutable too) which IMO is a better design when you can have millions of threads.</div><div dir="auto" style="color:rgb(0,0,0)"><br></div><div dir="auto" style="color:rgb(0,0,0)">That said, since the opentelemetry context is extensible, you can emulate the behavior of scoped values by implementing something else than ArrayBasedContext if needed. Likewise, you can implement a different storage than <a href="https://github.com/open-telemetry/opentelemetry-java/blob/main/context/src/main/java/io/opentelemetry/context/ThreadLocalContextStorage.java" target="_blank">https://github.com/open-telemetry/opentelemetry-java/blob/main/context/src/main/java/io/opentelemetry/context/ThreadLocalContextStorage.java</a> should you need to.</div><div dir="auto" style="color:rgb(0,0,0)"><br></div><div dir="auto" style="color:rgb(0,0,0)">Anyway, I think it does everything that you need. It would be very nice of you if you could copy my answer to the mailing list to bring awareness to others (I don't care if you cite me or not).</div><div dir="auto" style="color:rgb(0,0,0)"><br></div><div dir="auto" style="color:rgb(0,0,0)">I hope this helps.</div><div dir="auto" style="color:rgb(0,0,0)"><br></div><div dir="auto" style="color:rgb(0,0,0)">Cheers,</div><div dir="auto" style="color:rgb(0,0,0)">Jean-Noël </div></div><div dir="auto" style="color:rgb(0,0,0)">-----------------------------------------------------------------------</div><div dir="auto" style="color:rgb(0,0,0)"><br></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Sat, Jan 18, 2025 at 10:11 AM Andrew Haley <<a href="mailto:aph-open@littlepinkcloud.com">aph-open@littlepinkcloud.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 1/17/25 09:08, Davor Hrg wrote:<br>
> I am looking to see if someone has a workaround that uses ThreadLocals in such way that is compatible with scoped values. (I do not need for structured concurrency, just a stack inside a single thread)<br>
<br>
Sure, you just wrap ThreadLocal.set() and and ThreadLocal.remove() in a<br>
try-finally. Or doe you need something more than that?<br>
<br>
-- <br>
Andrew Haley  (he/him)<br>
Java Platform Lead Engineer<br>
Red Hat UK Ltd. <<a href="https://www.redhat.com" rel="noreferrer" target="_blank">https://www.redhat.com</a>><br>
<a href="https://keybase.io/andrewhaley" rel="noreferrer" target="_blank">https://keybase.io/andrewhaley</a><br>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671<br>
</blockquote></div>