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.