ConcurrentHashMap锁定 [英] ConcurrentHashMap locking
问题描述
我已经在 ConcurrentHashMap
中读到了某个地方,整个地图对象没有被锁定,而是在Map的一部分上进行了锁定。
I have read somewhere that in ConcurrentHashMap
, the whole map object is not locked and instead a lock is made on a portion of the Map.
有人可以详细说明锁定何时进入图片?
Can somebody elaborate when does locking come into the picture?
在阅读地图时是否正确是否没有锁定,但在更新时只使用锁定?
Is it right that while reading the Map there is no locking involved in it but while updating it only locking is used?
推荐答案
是, ConcurrentHashMap
使用多个锁(默认情况下,其中16个),每个锁控制一个哈希段。
Yes, ConcurrentHashMap
uses a multitude of locks (by default, 16 of them), each lock controls one segment of the hash.
在特定设置数据时段,获取该段的锁定。
When setting data in a particular segment, the lock for that segment is obtained.
获取数据时,使用易失性读取。如果易失性读取导致未命中,则获得该段的锁定以获得成功读取的最后一次尝试。
When getting data, a volatile read is used. If the volatile read results in a miss, then the lock for the segment is obtained for a last attempt at a successful read.
这篇关于ConcurrentHashMap锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!