ConcurrentHashMap in Java Lock Engine for computeIfPresent

I am using Java 8 and would like to know if the operation really computeIfPresent ConcurrentHashMaplocks the entire table / map or only the cell containing the key.

From the method documentationcomputeIfPresent :

Some attempts to perform update operations on this map by other threads may be blocked while the calculation is in progress, so the calculation should be short and simple and should not try to update any other mappings of this map.

It seems that the whole card is locked when this method is called for the key. Why should the whole card be locked if the value of a specific key is updated? Wouldn't it be better to just lock the cart containing the key / value pair?

+4
source share
2 answers

Judging by the implementation (Oracle JDK 1.8.0_101), only the corresponding bit is locked. This does not contradict the cited piece of documentation, as it mentions that some update operations may be blocked, and not necessarily all. Of course, it would be clearer if the documents clearly indicate what will be blocked, but it will be a leak of implementation details in the de facto part of the interface.

+1
source

If you look at the source code ConcurrentHashMap#computeIfPresent, you will notice that synchronization is done directly in the node.

, node, . .

, ConcurrentHashMap vs old Hashtable.

hashtables, , . , ConcurrentHashMap .

Hashtable , :

[...] Hashtable . - HashMap Hashtable. , ConcurrentHashMap Hashtable.

+1

Source: https://habr.com/ru/post/1668151/


All Articles