Gaurav Seth
Gaurav Seth

Reputation: 205

How to set concurrency level in concurrentHashMap in java

Please tell me how to set concurrency level in ConcurrentHashMap in java? What impact does it have if the level is set too high or too low? How does it work internally?

Upvotes: 1

Views: 2821

Answers (1)

veritas
veritas

Reputation: 2444

ConcurrentHashMap constructor

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

You can very well define the concurrency level as desired by you. Regarding your second question. Here is the javadoc comment from ConncurrentHashMap (java 1.6)

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

So ideally if you don't know things like how big the map could become or how many thread will be accessing the map , Better use default values of concurrency (which is currently 16).

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

Hope its help !

Upvotes: 2

Related Questions