Java 8:
現在、ConcurrentHashMap
は固定ロック ストライピング スキームをまったく使用しておらず、代わりに各バケットが固有の同期を使用して「ストライプ」として機能しています。
ソースからのコード:
/** 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) {
...
}
}
そして、コンストラクターには、ドキュメントが言うように、サイズのヒントとして使用するパラメーターがあります。
concurrencyLevel - 同時に更新するスレッドの推定数。実装では、この値をサイジングのヒントとして使用できます。
そしてソース:
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;
}
そのため、自分で考える必要はありませんConcurrentHashMap
。あなたに代わって処理します。