本文介绍了ConcurrentHashMap是否可以拥有32个以上的锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到ConcurrentHashMap在多线程中比Hashtable更好,这是因为在存储桶级别具有锁,而不是在映射范围内具有锁.每个地图最多可以锁定32个锁.想知道为什么32个锁,为什么不超过32个锁.

I read ConcurrentHashMap works better in multi threading than Hashtable due to having locks at bucket level rather than map wide lock. It is at max 32 locks possible per map. Want to know why 32 and why not more than 32 locks.

推荐答案

如果您正在谈论Java ConcurrentHashMap,则限制为任意:

If you're talking about the Java ConcurrentHashMap, then the limit is arbitrary:

如果您阅读了源代码很明显,段的最大数量为2 ^ 16,对于不久的将来可能出现的任何需求,这应该绰绰有余.

If you read the source code it becomes clear that the maximum number of segments is 2^16, which should be more than sufficient for any conceivable need in the immediate future.

您可能一直在考虑某些替代性实验实施,例如这一个:

You may have been thinking of certain alternative experimental implementations, like this one:

请注意,通常,当超过32个线程试图更新单个ConcurrentHashMap时,瓶颈通常不是同步效率.

Note that in general, factors other than synchronization efficiency are usually the bottlenecks when more than 32 threads are trying to update a single ConcurrentHashMap.

这篇关于ConcurrentHashMap是否可以拥有32个以上的锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:18