Does concurrenthashmap have the problem of dead circulation?

For the sake of insurance, the business code of the problem cannot be directly posted, so the problem is simplified into the following code:

Interested buddy can run on the computer. If not, say the problem first. When computeIfAbsent is executed, if the corresponding slot of key is empty, then the ReservationNode object (hash value RESERVED=-3) will be created at the current slot position and then mappingFunction. will be called. Apply (key) generates value, creates a node based on value, and assigns it to the slow position. At this time, the computeifabsent process is completed. However, the above code mappingfunction performs a put operation on the map and triggers the rehash operation. When traversing the slot array in the transfer, judge whether the node corresponding to the slot is null, whether the hash value is moved = - 1, whether the hash value is greater than 0 (list structure), and whether the node type is treebin (red black tree structure), except that the hash value is reserved = - 3, This leads to the problem of dead circulation.

After analyzing the problem, the reason is clear. At that time, we thought it might be a "bug" of JDK, so our final solution is:

Finally, another friend read the note of computeifabsent:

We found that in fact, people already know this problem and specially annotate it... We're still too yong too simple. So far, the deadlock problem of concurrenthashmap has come to an end. You should still follow the coding specification and do not update the current map in the mappingfunction. In fact, the concurrenthashmap loop not only appears in the scenarios discussed above, but also will be triggered in the following scenarios. The reason is the same as that discussed above. The code is as follows. Interested partners can also run locally:

Finally, follow the computeifabsent source code to divide the execution process of the above-mentioned dead loop code. Limited to space, only the main process codes are analyzed:

Recommended reading:

For more articles, you can scan the following QR codes:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>