如何在java中的concurrentHashMap中设置并发级别

时间:2013-06-30 18:35:09

标签: java concurrency concurrenthashmap

请告诉我如何在java中的ConcurrentHashMap中设置并发级别?如果水平设置得太高或太低,它会产生什么影响?它在内部如何运作?

1 个答案:

答案 0 :(得分: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

希望有所帮助!