How to use and set appropriately concurrency level for ConcurrentHashMap?
According to docs:
The allowed concurrency among update operations is guided by the optional
concurrencyLevel
constructor argument (default16
), which is used as a hint for internal sizing. The table is internally partitioned to try to permit the indicated number of concurrent updates without contention. Because placement in hash tables is essentially random, the actual concurrency will vary. 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 you need to answer 1 question:
What is the number of threads that will ever concurrently modify the table?
Java 8:
Now the ConcurrentHashMap
does not use a fixed lock striping scheme at all, instead each bucket serves as a “stripe” using intrinsic synchronization.
Code from source:
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {
...
Node<K,V> f; int n, i, fh;
...
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
...
synchronized (f) {
...
}
}
And the constructor has the parameter just use it as a size hint as docs say.
concurrencyLevel - the estimated number of concurrently updating threads. The implementation may use this value as a sizing hint.
And the source:
public ConcurrentHashMap(int initialCapacity,
float loadFactor, int concurrencyLevel) {
if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
throw new IllegalArgumentException();
if (initialCapacity < concurrencyLevel) // Use at least as many bins
initialCapacity = concurrencyLevel; // as estimated threads
long size = (long)(1.0 + (long)initialCapacity / loadFactor);
int cap = (size >= (long)MAXIMUM_CAPACITY) ?
MAXIMUM_CAPACITY : tableSizeFor((int)size);
this.sizeCtl = cap;
}
So you don't need to consider it by yourself, ConcurrentHashMap
will handle it for you.