ConcurrentHashMap在jdk7的使用的是分段鎖(ReentrantLock),而jdk8則改爲使用synchronized。同時jdk8的ConcurrentHashMap和HashMap一樣的引入紅黑樹(解決hash衝撞時的操作效率),並且在擴容過程中像ForkJoinPool一樣可以自動多線程協作(提高擴容效率,並且解決HashMap的擴容時併發問題……PS:請慎用jdk8的ParallelStream,因爲它底層默認調用的是公共的ForkJoinPool)。整個代碼的邏輯和HashMap十分相似,可以對照着看方便理解。
下文和HashMap一樣,針對ConcurrentHashMap的初始化、put和擴容源碼進行分析。
初始化
/**
* Creates a new, empty map with an initial table size
* accommodating the specified number of elements without the need
* to dynamically resize.
*
* @param initialCapacity The implementation performs internal
* sizing to accommodate this many elements.
* @throws IllegalArgumentException if the initial capacity of
* 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;
}
/**
* Returns a power of two table size for the given desired capacity.
* 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;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
其中的tableSizeFor(int c)和HashMap一致,並且都是將真正存放數據的內部Node數組延遲初始化。特殊的是最後一行this.sizeCtl = cap;
sizeCtl使用了Unsafe進行操縱,一樣使用Unsafe的還有以下這些變量。通過這些變量和相關代碼,ConcurrentHashMap實現高併發時線程安全的效果。
/**
* Base counter value, used mainly when there is no contention,
* but also as a fallback during table initialization
* races. Updated via CAS.
*/
private transient volatile long baseCount;
/**
* Table initialization and resizing control. When negative, the
* table is being initialized or resized: -1 for initialization,
* else -(1 + the number of active resizing threads). Otherwise,
* when table is null, holds the initial table size to use upon
* creation, or 0 for default. After initialization, holds the
* next element count value upon which to resize the table.
*/
private transient volatile int sizeCtl;
/**
* The next table index (plus one) to split while resizing.
*/
private transient volatile int transferIndex;
/**
* Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
*/
private transient volatile int cellsBusy;
// Unsafe mechanics
private static final sun.misc.Unsafe U;
private static final long SIZECTL;
private static final long TRANSFERINDEX;
private static final long BASECOUNT;
private static final long CELLSBUSY;
private static final long CELLVALUE;
private static final long ABASE;
private static final int ASHIFT;
static {
try {
U = sun.misc.Unsafe.getUnsafe();
Class<?> k = ConcurrentHashMap.class;
SIZECTL = U.objectFieldOffset
(k.getDeclaredField("sizeCtl"));
TRANSFERINDEX = U.objectFieldOffset
(k.getDeclaredField("transferIndex"));
BASECOUNT = U.objectFieldOffset
(k.getDeclaredField("baseCount"));
CELLSBUSY = U.objectFieldOffset
(k.getDeclaredField("cellsBusy"));
Class<?> ck = CounterCell.class;
CELLVALUE = U.objectFieldOffset
(ck.getDeclaredField("value"));
Class<?> ak = Node[].class;
ABASE = U.arrayBaseOffset(ak);
int scale = U.arrayIndexScale(ak);
if ((scale & (scale - 1)) != 0)
throw new Error("data type scale not a power of two");
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
} catch (Exception e) {
throw new Error(e);
}
}
put
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
static final int spread(int h) {
// 高低位異或,並且強制第一位爲0
return (h ^ (h >>> 16)) & HASH_BITS;
}
public V put(K key, V value) {
return putVal(key, value, false);
}
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
// 使用高低位異或得到內部使用的hash值
int hash = spread(key.hashCode());
int binCount = 0;
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
if (tab == null || (n = tab.length) == 0)
tab = initTable(); // 首次使用或者延遲實例化
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
// Node數組中hash對應位置f沒值,使用cas新建節點
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f); // Node數組中hash標記爲遷移中,協助遷移
else {
// hash對應位置已有節點f
V oldVal = null;
// 單獨對節點f加同步鎖
synchronized (f) {
// 再檢查一次
if (tabAt(tab, i) == f) {
if (fh >= 0) {
// 鏈表,最後binCount爲鏈表長度
binCount = 1;
for (Node<K,V> e = f;; ++binCount) {
K ek;
// hash值相同且key相同,準備賦值value
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
oldVal = e.val;
if (!onlyIfAbsent)
e.val = value;
break;
}
// 到達鏈表末尾,添加新node
Node<K,V> pred = e;
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
else if (f instanceof TreeBin) {
// 判斷節點是紅黑樹,插入紅黑樹
Node<K,V> p;
binCount = 2;
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
if (binCount != 0) {
// 鏈表長度達到紅黑樹閾值,轉換爲紅黑樹
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
// 計數器+1,並且檢查是否需要擴容
addCount(1L, binCount);
return null;
}
/**
* Replaces all linked nodes in bin at given index unless table is
* too small, in which case resizes instead.
*/
private final void treeifyBin(Node<K,V>[] tab, int index) {
Node<K,V> b; int n, sc;
if (tab != null) {
// node數組長度小於64時,和HashMap一樣進行擴容代替轉換紅黑樹操作
if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
tryPresize(n << 1);
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
// 節點b存在且狀態不是在擴容中,加同步鎖
synchronized (b) {
if (tabAt(tab, index) == b) {
TreeNode<K,V> hd = null, tl = null;
// 將鏈表的每個元素按序轉換爲紅黑樹的一個節點
for (Node<K,V> e = b; e != null; e = e.next) {
TreeNode<K,V> p =
new TreeNode<K,V>(e.hash, e.key, e.val,
null, null);
if ((p.prev = tl) == null)
hd = p;
else
tl.next = p;
tl = p;
}
// 將樹直接設置到node數組中
setTabAt(tab, index, new TreeBin<K,V>(hd));
}
}
}
}
}
擴容
有三個方法觸發擴容,分別是addCount、helpTransfer和tryPresize,對應外層的各種添加元素操作,下面主要分析transfer這個擴容的核心方法。
transfer
/** Number of CPUS, to place bounds on some sizings */
static final int NCPU = Runtime.getRuntime().availableProcessors();
/**
* Minimum number of rebinnings per transfer step. Ranges are
* subdivided to allow multiple resizer threads. This value
* serves as a lower bound to avoid resizers encountering
* excessive memory contention. The value should be at least
* DEFAULT_CAPACITY.
*/
private static final int MIN_TRANSFER_STRIDE = 16;
/**
* Moves and/or copies the nodes in each bin to new table. See
* above for explanation.
*/
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
int n = tab.length, stride;
// 通過CPU核數進行擴容任務分割,最少分割爲16個子任務
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
stride = MIN_TRANSFER_STRIDE; // subdivide range
if (nextTab == null) { // initiating
// 初始化擴容用的新node數組,擴容1倍
try {
@SuppressWarnings("unchecked")
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
nextTab = nt;
} catch (Throwable ex) { // try to cope with OOME
sizeCtl = Integer.MAX_VALUE;
return;
}
nextTable = nextTab;
transferIndex = n;
}
int nextn = nextTab.length;
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
boolean advance = true;
boolean finishing = false; // to ensure sweep before committing nextTab
for (int i = 0, bound = 0;;) {
Node<K,V> f; int fh;
while (advance) {
int nextIndex, nextBound;
if (--i >= bound || finishing)
advance = false;
else if ((nextIndex = transferIndex) <= 0) {
i = -1;
advance = false;
}
else if (U.compareAndSwapInt
(this, TRANSFERINDEX, nextIndex,
nextBound = (nextIndex > stride ?
nextIndex - stride : 0))) {
// 從最後一個節點開始進行倒序遷移,子任務完成後,剩餘的節點都由本線程獨立進行遷移
bound = nextBound;
i = nextIndex - 1;
advance = false;
}
}
if (i < 0 || i >= n || i + n >= nextn) {
int sc;
if (finishing) {
// 擴容完成,替換正式node數組
nextTable = null;
table = nextTab;
// sizeCtl設置爲當前數組大小的0.75(1-1/4=0.75)
sizeCtl = (n << 1) - (n >>> 1);
return;
}
if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
return;
finishing = advance = true;
i = n; // recheck before commit
}
}
else if ((f = tabAt(tab, i)) == null)
// 原f節點爲null,直接標記節點已遷移
advance = casTabAt(tab, i, null, fwd);
else if ((fh = f.hash) == MOVED)
// f節點已遷移,執行下一個節點的遷移
advance = true; // already processed
else {
// 鎖定f節點,進行遷移
synchronized (f) {
// 再校驗
if (tabAt(tab, i) == f) {
Node<K,V> ln, hn;
if (fh >= 0) {
// 鏈節點
int runBit = fh & n;
Node<K,V> lastRun = f;
for (Node<K,V> p = f.next; p != null; p = p.next) {
int b = p.hash & n;
if (b != runBit) {
runBit = b;
lastRun = p;
}
}
// 同HashMap擴容的鏈表處理,將數組中f節點的元素按原順序分散到新Node數組上
if (runBit == 0) {
ln = lastRun;
hn = null;
}
else {
hn = lastRun;
ln = null;
}
for (Node<K,V> p = f; p != lastRun; p = p.next) {
int ph = p.hash; K pk = p.key; V pv = p.val;
if ((ph & n) == 0)
ln = new Node<K,V>(ph, pk, pv, ln); // 掩碼位爲0,保留在低位鏈表
else
hn = new Node<K,V>(ph, pk, pv, hn); // 掩碼位爲1,添加到高位鏈表
}
// 將高低位鏈表分別放到新node數組中
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
// 原node數組f節點標記爲已遷移
setTabAt(tab, i, fwd);
advance = true;
}
else if (f instanceof TreeBin) {
// 樹節點
TreeBin<K,V> t = (TreeBin<K,V>)f;
TreeNode<K,V> lo = null, loTail = null;
TreeNode<K,V> hi = null, hiTail = null;
int lc = 0, hc = 0;
// 類似鏈節點的處理,將數組中f節點的元素按原順序分散到新Node數組上
for (Node<K,V> e = t.first; e != null; e = e.next) {
int h = e.hash;
TreeNode<K,V> p = new TreeNode<K,V>
(h, e.key, e.val, null, null);
if ((h & n) == 0) {
// 掩碼位爲0,保留在低位樹
if ((p.prev = loTail) == null)
lo = p;
else
loTail.next = p;
loTail = p;
++lc;
}
else {
// 掩碼位爲1,添加到高位樹
if ((p.prev = hiTail) == null)
hi = p;
else
hiTail.next = p;
hiTail = p;
++hc;
}
}
// 分散後得到的高低位樹長度小於等於6的,轉換爲鏈表
ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
(hc != 0) ? new TreeBin<K,V>(lo) : t;
hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
(lc != 0) ? new TreeBin<K,V>(hi) : t;
// 將高低位樹分別放到新node數組中
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
// 原node數組f節點標記爲已遷移
setTabAt(tab, i, fwd);
advance = true;
}
}
}
}
}
}
/**
* A node inserted at head of bins during transfer operations.
*/
static final class ForwardingNode<K,V> extends Node<K,V> {
final Node<K,V>[] nextTable;
ForwardingNode(Node<K,V>[] tab) {
// 設置hash爲MOVED,用於判斷該節點是否已經完成遷移
super(MOVED, null, null, null);
this.nextTable = tab;
}
……
}
可見和jdk8的HashMap擴容區別不大,使用了同步鎖、CAS、倒序遷移、劃分子任務等技術實現了多線程安全和高效。
helpTransfer
/**
* Helps transfer if a resize is in progress.
*/
final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
Node<K,V>[] nextTab; int sc;
if (tab != null && (f instanceof ForwardingNode) &&
(nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
int rs = resizeStamp(tab.length);
while (nextTab == nextTable && table == tab &&
(sc = sizeCtl) < 0) {
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
sc == rs + MAX_RESIZERS || transferIndex <= 0)
break;
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
// 擴容中但未擴容完成的,協助擴容
transfer(tab, nextTab);
break;
}
}
return nextTab;
}
return table;
}