ConcurrentHashMap 是否可以有超过 32 个锁

发布于 2024-08-12 06:11:25 字数 112 浏览 5 评论 0原文

我读到 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍景流年 2024-08-19 06:11:25

如果您谈论的是 Java ConcurrentHashMap,那么限制是 任意

创建一个与给定地图具有相同映射的新地图。创建的映射容量为给定映射中映射数量的 1.5 倍或 16(以较大者为准),以及默认负载因子 (0.75) 和并发级别 (16)。

如果您阅读源代码,它就会变得清楚最大段数为 2^16,这应该足以满足近期任何可能的需求。

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

此类支持硬连线的预设并发级别 32。这允许最多同时进行 32 个放置和/或删除操作。

请注意,一般来说,当超过 32 个线程尝试更新单个 ConcurrentHashMap 时,同步效率以外的因素通常会成为瓶颈。

If you're talking about the Java ConcurrentHashMap, then the limit is 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).

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:

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.

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.

从来不烧饼 2024-08-19 06:11:25

默认值不是 32,而是 16。您可以使用 构造函数参数并发级别

public ConcurrentHashMap(int initialCapacity,
                         float loadFactor,
                         int concurrencyLevel)

所以你可以这样做:

Map<String, String> map = new ConcurrentHashmap<String, String)(128, 0.75f, 64);

将其更改为64。默认值是(从Java 6u17开始):

  • initialCapacity:16;
  • loadFactory:0.75f;
  • 并发级别:16。

The default isn't 32, it's 16. And you can override it with the constructor argument concurrency level:

public ConcurrentHashMap(int initialCapacity,
                         float loadFactor,
                         int concurrencyLevel)

so you can do:

Map<String, String> map = new ConcurrentHashmap<String, String)(128, 0.75f, 64);

to change it to 64. The defaults are (as of Java 6u17):

  • initialCapacity: 16;
  • loadFactory: 0.75f;
  • concurrencyLevel: 16.
木有鱼丸 2024-08-19 06:11:25

根据ConcurrentHashMap的来源,允许的最大值是65536

/**
 * The maximum number of segments to allow; used to bound
 * constructor arguments.
 */
static final int MAX_SEGMENTS = 1 << 16; // slightly conservative

public ConcurrentHashMap(int initialCapacity,
                         float loadFactor, int concurrencyLevel) {
    if (concurrencyLevel > MAX_SEGMENTS)
        concurrencyLevel = MAX_SEGMENTS;

According to the source of ConcurrentHashMap, the maximum allowed is 65536:

/**
 * The maximum number of segments to allow; used to bound
 * constructor arguments.
 */
static final int MAX_SEGMENTS = 1 << 16; // slightly conservative

public ConcurrentHashMap(int initialCapacity,
                         float loadFactor, int concurrencyLevel) {
    if (concurrencyLevel > MAX_SEGMENTS)
        concurrencyLevel = MAX_SEGMENTS;
幸福不弃 2024-08-19 06:11:25

要使用所有默认并发级别 16,您需要有 16 个核心同时使用该映射。如果您有 32 个核心,仅在 25% 的时间内使用该映射,则一次将仅使用 16 个段中的 8 个。

总之,您需要有很多核心都使用相同的映射,并且不执行任何其他操作。真实的程序通常会执行除访问一张映射之外的其他操作。

To use all the default concurrency level of 16 you need to have 16 cores using the map at the same moment. If you have 32 cores only using the map 25% of the time then only 8 of 16 segments will be used at once.

In summary, you need to have a lot of cores all using the same map and doing nothing much else. Real programs usually do something other than access one map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文