ConcurrentHashMap是否可以拥有32个以上的锁 [英] Is it possible to have more than 32 locks in ConcurrentHashMap

查看:92
本文介绍了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:

使用与给定映射相同的映射创建一个新映射.创建的地图的容量是给定地图中映射数的1.5倍或16(以较大者为准),并具有默认的加载因子(0.75)和concurrencyLevel(16).

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).

如果您阅读了源代码很明显,段的最大数量为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.这最多允许32个放置和/或删除操作同时进行.

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时,瓶颈通常不是同步效率.

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个以上的锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆