简体   繁体   English

当concurrencyLevel大于ConcurrentHashMap的容量时,会发生什么?

[英]What happens when the concurrencyLevel is greater that the capacity of a ConcurrentHashMap?

I have been scratching my head around a question for some time now, I have looked around but have failed at finding the answer. 我已经在某个问题上挠头了一段时间,虽然环顾四周,但未能找到答案。 I want to know what will happen if the concurrencyLevel will be greater than the capacity of the Map. 我想知道如果concurrencyLevel大于Map的capacity会发生什么。

By default both are 16 which means that each bucket will have a lock. 默认情况下两者均为16,这意味着每个存储桶将具有一个锁。 And if the capacity will be 32 and concurrencyLevel 16 that a lock will be held on 2 buckets. 如果capacity为32, concurrencyLevel 16,则将在2个存储桶中保留一个锁。 But what happens when concurrencyLevel is 32 and capacity is 16? 但是,当concurrencyLevel为32且capacity为16时会发生什么?

Will each bucket be held by 2 locks, then what happens if the distribution of concurrencyLevel and capacity is uneven, like 24 and 16, or something? 将每个桶2个锁举行,那么如果分配会发生什么concurrencyLevelcapacity是不平衡的,如24和16,还是什么?

Nothing special happens. 没什么特别的。 Did you check the documentation of ConcurrentHashMap ? 您是否检查了ConcurrentHashMap的文档? It says: “Also, for compatibility with previous versions of this class, constructors may optionally specify an expected concurrencyLevel as an additional hint for internal sizing.” It's a hint, not a limitation. 它说:“此外,为了与此类的早期版本兼容,构造函数可以选择指定期望的concurrencyLevel作为内部调整的附加提示。”这是一个提示,而不是限制。 Furthermore on concurrencyLevel : “the estimated number of concurrently updating threads. 此外,在concurrencyLevel :“并发更新线程的估计数量。 The implementation may use this value as a sizing hint.” Note: “may”. 该实现可以将该值用作大小调整提示。”注意:“可以”。 So if anything happens, the size becomes different. 因此,如果发生任何事情,大小将变得不同。 Maybe. 也许。

If you want further implementation details, you may study the source code. 如果需要进一步的实现细节,可以研究源代码。 It's available. 有空 In Java 1.8 it contanis the following two lines: 在Java 1.8中,它包含以下两行:

    if (initialCapacity < concurrencyLevel)   // Use at least as many bins
        initialCapacity = concurrencyLevel;   // as estimated threads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM