0

请告诉我如何在 Java 的 ConcurrentHashMap 中设置并发级别?水平设置过高或过低会有什么影响?它在内部是如何工作的?

4

1 回答 1

2

ConcurrentHashMap 构造函数

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

您可以根据自己的需要很好地定义并发级别。关于你的第二个问题。这是来自 ConncurrentHashMap (java 1.6) 的 javadoc 注释

 * Ideally, you should choose a value to accommodate as many
 * threads as will ever concurrently modify the table. Using a
 * significantly higher value than you need can waste space and time,
 * and a significantly lower value can lead to thread contention.

因此,理想情况下,如果您不知道地图会变得多大或将有多少线程访问地图,最好使用并发的默认值(目前为 16)。

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/concurrent/ConcurrentHashMap.java

希望它的帮助!

于 2013-07-03T04:31:07.753 回答