我读到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时,除了同步效率之外的因素通常是瓶颈.