Reputation: 205
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
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).
Hope its help !
Upvotes: 2