8202422: value of 'sizeCtl' in ConcurrentHashMap varies with the constructor called

Reviewed-by: martin, psandoz
This commit is contained in:
Doug Lea 2018-06-25 09:59:16 -07:00
parent 7ca4027957
commit abffccb329
2 changed files with 64 additions and 23 deletions

View file

@ -702,12 +702,7 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
* See Hackers Delight, sec 3.2
*/
private static final int tableSizeFor(int c) {
int n = c - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
int n = -1 >>> Integer.numberOfLeadingZeros(c - 1);
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
@ -844,12 +839,7 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
* elements is negative
*/
public ConcurrentHashMap(int initialCapacity) {
if (initialCapacity < 0)
throw new IllegalArgumentException();
int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
MAXIMUM_CAPACITY :
tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
this.sizeCtl = cap;
this(initialCapacity, LOAD_FACTOR, 1);
}
/**
@ -883,8 +873,8 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
/**
* Creates a new, empty map with an initial table size based on
* the given number of elements ({@code initialCapacity}), table
* density ({@code loadFactor}), and number of concurrently
* the given number of elements ({@code initialCapacity}), initial
* table density ({@code loadFactor}), and number of concurrently
* updating threads ({@code concurrencyLevel}).
*
* @param initialCapacity the initial capacity. The implementation
@ -1473,13 +1463,9 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
if (size == 0L)
sizeCtl = 0;
else {
int n;
if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
n = MAXIMUM_CAPACITY;
else {
int sz = (int)size;
n = tableSizeFor(sz + (sz >>> 1) + 1);
}
long ts = (long)(1.0 + size / LOAD_FACTOR);
int n = (ts >= (long)MAXIMUM_CAPACITY) ?
MAXIMUM_CAPACITY : tableSizeFor((int)ts);
@SuppressWarnings("unchecked")
Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
int mask = n - 1;