<div dir="ltr">Hi.<div><br></div><div>Thank you for the information, that's good to know.</div><div><br></div><div>Just to be sure that I've understood the problem correctly, here's my example code:</div><div><br></div><div>public class C2 {<br>    private static final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>();<br><br>    private static String refresh(String key) {<br>        try (var scope = new StructuredTaskScope.ShutdownOnSuccess<String>()) {<br>            scope.fork(() -> UUID.randomUUID().toString());<br>            scope.join();<br>            return scope.result();<br>        } catch (Exception e) {<br>            throw new RuntimeException(e);<br>        }<br>    }<br><br>    public static void main(String[] args) throws Exception {<br>        var cpus = Runtime.getRuntime().availableProcessors();<br>        List<Future> fl = new ArrayList<>();<br>        try (var es = Executors.newVirtualThreadPerTaskExecutor()) {<br>            for (int i = 0; i < cpus; ++i)<br>                fl.add(es.submit(() -> cache.computeIfAbsent("foo", k -> refresh(k))));<br>        }<br>        for (var f : fl)<br>            System.out.println(f.get());<br>    }<br>}<br></div><div><br></div><div>If you set the "cpus" to  Runtime.getRuntime().availableProcessors() - 1 it completes, otherwise it hangs.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jan 20, 2023 at 2:49 PM Alan Bateman <<a href="mailto:Alan.Bateman@oracle.com">Alan.Bateman@oracle.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 20/01/2023 06:22, mikmoila wrote:<br>
> Hi.<br>
><br>
> As often mentioned in this mailing-list a feedback about <br>
> preview/incubator features is appreciated, so here's one:<br>
><br>
> I was experimenting with a caching system utilising ConcurrentHashMap <br>
> as cache store and Structured Concurrency API for refreshing the <br>
> entries from multiple sources ( StructuredTaskScope.ShutdownOnSuccess <br>
> ). The idea was to make http-requests for getting the fresh values but <br>
> the first implementation simply uses UUID::randomUUID for simulating that.<br>
> I noticed that the programs halts In a test case where "N" concurrent <br>
> calls (where "N" >= number of cpu cores) running on virtual threads <br>
> end-up calling the ConcurrentHashMap::computeIfAbsent for the same <br>
> (non-existing) key.<br>
><br>
> "-Djdk.tracePinnedThreads=full" reveals that there is a pinned carrier <br>
> thread:<br>
> java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) <br>
> <== monitors:1<br>
><br>
> The documentation ( <br>
> <a href="https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent(K,java.util.function.Function)" rel="noreferrer" target="_blank">https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent(K,java.util.function.Function)</a> <br>
> ) says:<br>
><br>
>   "Some attempted update operations on this map by other threads may <br>
> be blocked while computation is in progress, so the computation should <br>
> be short and simple."<br>
><br>
> This is clear but I still found it as a surprise that it uses <br>
> synchronized instead of "virtual-thread friendly" constructs.<br>
><br>
Thanks for taking time to send feedback.<br>
<br>
The CHM.computeXXX methods work okay from virtual threads when the <br>
mapping function does something short/simple. The quality of <br>
implementation issue is when they do something long running, like block <br>
on I/O, in which case you get disappointing performance, as you would <br>
with platform threads too.<br>
<br>
I think Doug Lea has looked at the synchronization in this code a few <br>
times. Separately, we expect the pinning issus will go away but it's not <br>
a trivial project and requires work in several areas.<br>
<br>
-Alan<br>
<br>
<br>
</blockquote></div>