gpt4 book ai didi

java - 增加并发hashmap分段

转载 作者:行者123 更新时间:2023-12-01 22:25:50 25 4
gpt4 key购买 nike

我正在查看并发 HashMap ,据我目前所知,它们默认分为 16 个部分。每个部分都有自己的键值对份额。如果一个线程想要持有一个键值对的锁,它将锁定整个段。 (如果我有什么地方错了,请纠正我)。现在在这个link有人提到,当作者较少时,chm 就很好。我想知道我们是否增加段的数量,我的意思是使其与数字键值对相当,是否可以为在同一 CHM 上操作的大量写入器线程创建并发性。另外,由于 CHM 中存在锁,因此内存消耗的成本有多大。

谢谢

贾延德拉

最佳答案

如果我正确理解你的问题,它在 JavaDoc 中得到了正确的回答。 :

The allowed concurrency among update operations is guided by the optional concurrencyLevel constructor argument (default 16), which is used as a hint for internal sizing. The table is internally partitioned to try to permit the indicated number of concurrent updates without contention. Because placement in hash tables is essentially random, the actual concurrency will vary. Ideally, you should choose a value to accommodate as many threads as will ever concurrently modify the table. Using a significantly higher value than you need can waste space and time, and a significantly lower value can lead to thread contention. But overestimates and underestimates within an order of magnitude do not usually have much noticeable impact. A value of one is appropriate when it is known that only one thread will modify and all others will only read. Also, resizing this or any other kind of hash table is a relatively slow operation, so, when possible, it is a good idea to provide estimates of expected table sizes in constructors.

关于java - 增加并发hashmap分段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28799373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com