2

Lets compare java doc snippets about compute method for interface ConcurrentMap and class ConcurrentHashMap

ConcurrentMap

The default implementation is equivalent to performing the following steps for this {@code map}, then returning the current value or {@code null} if absent:
...
The default implementation may retry these steps when multiple threads attempt updates including potentially calling the remapping function multiple times.

ConcurrenthashMap:

Attempts to compute a mapping for the specified key and its current mapped value (or {@code null} if there is no current mapping). The entire method invocation is performed atomically. 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, and must not attempt to update any other mappings of this Map.

I understand that current version of Jdk ConcurrentHashMap blocks the full segment and 2 compute actions for keys from same segment can't be executed in parallel but I am not sure can I use this knowledge in my code or not. In future oracle developers can change ConcurrentHashMap implementation(use CAS instead of blocking).

Please, share your thoughts.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • It's very unlikely they'd break backwards compatibility in such a huge way. – Kayaman Aug 31 '17 at 11:05
  • 2
    Doc for ConcurrentHashmap seems detailed enough to answer "No", now and in future. Remember that basic `ConcurrentMap` must support implementations other than ConcurrentHashMap, e.g. skip lists. As a side note: It sounds like your operation has a side effect, which should be avoided due to complexity and concurrency. – Markus Kull Aug 31 '17 at 12:19
  • The current version is JDK8, in which the implementation does *not* “blocks the full segment” as there is no such thing as a segment. It blocks the node, which represents a single key in the best case. – Holger Sep 06 '17 at 11:06
  • @Holger can you provide proof? – gstackoverflow Sep 06 '17 at 16:31
  • Compare the class documentations regarding what they say about “`concurrencyLevel `”: [Java 7](https://docs.oracle.com/javase/7/docs/api/?java/util/concurrent/ConcurrentHashMap.html): *The table is internally partitioned to try to permit the indicated number of concurrent updates without contention*… [Java 8](https://docs.oracle.com/javase/8/docs/api/?java/util/concurrent/ConcurrentHashMap.html): *Also, for compatibility with previous versions of this class, constructors may optionally specify an expected concurrencyLevel as an additional hint for internal sizing.* – Holger Sep 06 '17 at 16:37
  • You may also compare with [this answer](https://stackoverflow.com/a/31581438/2711488) or [the source code](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/concurrent/ConcurrentHashMap.java). Note that in this implementation, the class `Segment` has been merely “*declared for the sake of serialization compatibility*”… – Holger Sep 06 '17 at 16:51

0 Answers0