ConcurrentHashMap::computeIfAbsent and synchronized
mikmoila
mika.a.moilanen at gmail.com
Fri Jan 20 06:22:12 UTC 2023
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.
If needed I can post a small demo program.
JDK used is latest OpenJDK-19 GA, OS is Windows:
openjdk version "19.0.2" 2023-01-17
OpenJDK Runtime Environment (build 19.0.2+7-44)
OpenJDK 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)
Best Regards,
Mika
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230120/f8acc489/attachment.htm>
More information about the loom-dev
mailing list