gpt4 book ai didi

java - ConcurrentHashMap 的 concurrencyLevel 参数给我们什么保证?

转载 作者:行者123 更新时间:2023-12-04 05:33:59 25 4
gpt4 key购买 nike

来自 ConcurrentHashMap 的 Javadocs :

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.



我不明白说“用作内部尺寸提示”的部分。 .这是什么意思?设置这个值的最佳实践是什么,它给了我们什么保证?

最佳答案

看看 Javadoc 中接下来的句子:

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.



因此换句话说,concurencyLevel 为 16 意味着 ConcurrentHashMap 在内部创建了 16 个单独的哈希表来存储数据。修改一个哈希表中数据的操作不需要锁定其他哈希表,这允许对整个 Map 进行某种程度的并发访问。

您可能想尝试阅读 the source of ConcurrentHashMap .

关于java - ConcurrentHashMap 的 concurrencyLevel 参数给我们什么保证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12200001/

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