加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在ConcurrentHashMap中可以有超过32个锁

发布时间:2020-12-15 02:49:20 所属栏目:Java 来源:网络整理
导读:我读到ConcurrentHashMap在多线程方面比Hashtable更好,因为在桶级别而不是映射宽锁定.每张地图最多可锁32次.想知道为什么32和为什么不超过32锁. 解决方法 如果您正在谈论 Java ConcurrentHashMap,那么限制为 arbitrary: Creates a new map with the same ma
我读到ConcurrentHashMap在多线程方面比Hashtable更好,因为在桶级别而不是映射宽锁定.每张地图最多可锁32次.想知道为什么32和为什么不超过32锁.

解决方法

如果您正在谈论 Java ConcurrentHashMap,那么限制为 arbitrary:

Creates a new map with the same mappings as the given map. The map is created with a capacity of 1.5 times the number of mappings in the given map or 16 (whichever is greater),and a default load factor (0.75) and concurrencyLevel (16).

如果您阅读了source code,那么很明显,最大段数是2 ^ 16,这对于不久的将来任何可能的需求都应该足够了.

您可能一直在考虑某些替代实验性实现,如this one:

This class supports a hard-wired preset concurrency level of 32. This allows a maximum of 32 put and/or remove operations to proceed concurrently.

请注意,通常,当超过32个线程尝试更新单个ConcurrentHashMap时,除了同步效率之外的因素通常是瓶颈.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读