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