本文基于JDK1.8源碼驰徊。
簡述
- ConcurrentHashMap是線程安全的HashMap實現(xiàn)秤茅,底層通過一個table(Node[]數(shù)組)維護hash槽。table的size會被強制設(shè)定為2的冪性穿,負載因子默認為0.75读恃,通過sizeCtl記錄當(dāng)前負載因子下擴容閾值。
- hash槽的計算通過hash值的高16位與低16位做^運算后囤采,再和table的size做&運算計算而得述呐。
- ConcurrentHashMap通過Unsafe+CAS+Sychronized+Node實現(xiàn)線程安全,在真正轉(zhuǎn)移的時候都會使用sychronized進行加鎖蕉毯。
那些你不得不知道的節(jié)點
Node
ForwordingNode市埋、TreeBin、TreeNode的父類恕刘,核心的字段就是hash值缤谎、key、value和用于在鏈表中指向下一個節(jié)點的next褐着。
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
volatile V val;
volatile Node<K,V> next;
Node(int hash, K key, V val, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.val = val;
this.next = next;
}
...
}
ForwordingNode
hash值為-1坷澡,代表當(dāng)前的table正在進行復(fù)制遷移,同時標識當(dāng)前槽位的節(jié)點已經(jīng)完成遷移工作含蓉。ConcurrentHashMap充分發(fā)揮每個線程的作用频敛,當(dāng)T1線程正在遷移時,其它線程如T2不一定自旋等待馅扣,根據(jù)當(dāng)前遷移工作分配情況斟赚,可能會協(xié)助進行復(fù)制遷移(具體由CPU數(shù)量、table大小差油、遷移進度等決定)拗军。
可以發(fā)現(xiàn)ForwardingNode的構(gòu)造函數(shù)中調(diào)用父類的構(gòu)造函數(shù),有效參數(shù)只有hash值為MOVED(-1)蓄喇,同時用nextTable指向擴容后的數(shù)組发侵。
static final class ForwardingNode<K,V> extends Node<K,V> {
final Node<K,V>[] nextTable;
// 構(gòu)造函數(shù)調(diào)用
ForwardingNode(Node<K,V>[] tab) {
super(MOVED, null, null, null);
this.nextTable = tab;
}
...
}
TreeBin
hash值為-2,保存指向紅黑樹根節(jié)點的句柄root妆偏。同時保存原有鏈表的第一個元素first刃鳄。
當(dāng)一個槽位上的Node是TreeBin,這便代表著這個槽位長度已經(jīng)超過8钱骂,并且將鏈表初始化為紅黑樹叔锐。
// 這里的入?yún)門reeBin的first變量
static <K,V> Node<K,V> untreeify(Node<K,V> b) {
Node<K,V> hd = null, tl = null;
for (Node<K,V> q = b; q != null; q = q.next) {
Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
if (tl == null)
hd = p;
else
tl.next = p;
tl = p;
}
return hd;
}
TreeNode
紅黑樹節(jié)點挪鹏。在鏈表轉(zhuǎn)化為紅黑樹后,由于TreeNode繼承Node愉烙,所以依然保留了樹化之前的鏈表關(guān)系狰住,以便當(dāng)節(jié)點數(shù)小于8時,重新轉(zhuǎn)換為鏈表齿梁。
除了繼承Node的屬性外耕漱,另有代表紅黑樹特性的屬性:parent指向父節(jié)點患整、left指向左節(jié)點砖顷、right指向右節(jié)點涩嚣、布爾值red代表當(dāng)前節(jié)點是紅色還是黑色(根節(jié)點和葉子節(jié)點都是黑色)。
static final class TreeNode<K,V> extends Node<K,V> {
TreeNode<K,V> parent; // red-black tree links
TreeNode<K,V> left;
TreeNode<K,V> right;
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red;
TreeNode(int hash, K key, V val, Node<K,V> next,
TreeNode<K,V> parent) {
super(hash, key, val, next);
this.parent = parent;
}
}
如何計算hash槽位
// 計算hash值 h為hashCode
// HASH_BITS值為(1<<31)-1 = Integer.MAX_VALUE = 0x7fffffff
// (高16位^低16位) & HASH_BITS
static final int spread(int h) {
return (h ^ (h >>> 16)) & HASH_BITS;
}
final V putVal(K key, V value, boolean onlyIfAbsent) {
...
if (key == null || value == null) throw new NullPointerException();
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();
// hash值和table的size-1 做&運算計算得到hash槽位的index
else if (f = tabAt(tab, i = (n - 1) & hash)) == null) {
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
...
}
...
}
擴容還是轉(zhuǎn)樹
在ConcurretnHashMap中當(dāng)一個槽位的元素數(shù)到達8后省核,會判斷當(dāng)前table的長度是否小于64稿辙,如果長度小于64會優(yōu)先進行數(shù)組擴容。
初始化數(shù)組initTable
ConcurrentHashMap的數(shù)組為懶加載气忠,在調(diào)用放置元素之前不會初始化table邻储。當(dāng)sizeCtl為-1,表示有線程正在初始化table旧噪。
private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
// 確認table還未初始化
while ((tab = table) == null || tab.length == 0) {
// 如果sizeCtl<0, 表示當(dāng)前線程在初始化競爭中失敗吨娜,自旋等待(yield會放棄CPU時間片)
if ((sc = sizeCtl) < 0)
Thread.yield(); // lost initialization race; just spin
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
//SIZECTL:表示當(dāng)前對象的內(nèi)存偏移量,sc表示期望值淘钟,-1表示要替換的值宦赠,設(shè)定為-1表示要初始化表了
try {
if ((tab = table) == null || tab.length == 0) {
int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
@SuppressWarnings("unchecked")
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
table = tab = nt;
// 這里sc賦值為3/4的數(shù)組長度
sc = n - (n >>> 2);
}
} finally {
sizeCtl = sc;
}
break;
}
}
return tab;
}
put(K key, V value, boolean onlyIfAbsent)
注意擴容包括,數(shù)組擴容和對應(yīng)位置的樹化米母。根據(jù)源碼可以發(fā)現(xiàn)在添加元素的時候有大致四種情況:
- 數(shù)組尚未初始化勾扭,需要先初始化√鳎可能同時存在一個線程也在put妙色,這時候,兩個線程會競爭下初始化數(shù)組的權(quán)利慧耍,失敗的yield()自旋等待身辨。
- 數(shù)組已經(jīng)初始化,對應(yīng)hash槽沒有元素蜂绎,通過CAS操作添加節(jié)點栅表。值得注意的是笋鄙,往空槽加元素的時候不會加鎖师枣。
- 數(shù)組正在擴容復(fù)制數(shù)據(jù),當(dāng)前線程會幫忙復(fù)制數(shù)據(jù)萧落,幫忙前會判斷nextTab是否已經(jīng)初始化践美。
- 數(shù)組已經(jīng)初始化完成洗贰,沒有在擴容復(fù)制數(shù)據(jù),對應(yīng)hash槽非空陨倡。很好敛滋,又有兩種情況,hash槽對應(yīng)的仍然是鏈表兴革,或者已經(jīng)樹化绎晃。
- 仍然是鏈表:遍歷鏈表,替換舊值(非putIfAbsent)杂曲。沒有舊值庶艾,就把元素放到最后。同時更新binCount擎勘,后續(xù)判斷是否需要樹化咱揍。
- 已經(jīng)樹化:設(shè)置binCount=2,避免無效樹化棚饵。調(diào)用putTreeVal()獲取待更新節(jié)點煤裙。
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;
// 構(gòu)造函數(shù)中不會初始化table,在初次使用時進行初始化數(shù)組
if (tab == null || (n = tab.length) == 0)
tab = initTable();
// 數(shù)組已經(jīng)初始化噪漾,并且對應(yīng)位置無元素硼砰,直接放進去
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
// 上個判斷中獲得了目標位置的節(jié)點,如果hash值為MOVED欣硼,
// 代表當(dāng)前正在進行擴容的數(shù)據(jù)復(fù)制階段夺刑,當(dāng)前線程也會參與,
// 允許多線程復(fù)制的功能,來減少數(shù)組的復(fù)制所帶來的性能損失
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
synchronized (f) {
if (tabAt(tab, i) == f) {
// fh>0 還是鏈表 轉(zhuǎn)為樹的時候是-2
if (fh >= 0) {
// 對應(yīng)位置節(jié)點不為null分别,
binCount = 1;
// 開始遍歷鏈表遍愿,直到找到對應(yīng)
for (Node<K,V> e = f;; ++binCount) {
K ek;
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
// 根據(jù)hash值找到對應(yīng)的key位置,如果不是putIfAbsent就進行value替換
// 記下oldValue用于返回
oldVal = e.val;
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
// 沒找到原有值耘斩,且已經(jīng)到隊列尾巴沼填,直接加到末尾
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
// 當(dāng)前hash槽對應(yīng)的是樹
else if (f instanceof TreeBin) {
Node<K,V> p;
// 這里設(shè)置為2,確保后面不會樹化括授?
binCount = 2;
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
// 因為是put方法坞笙,所以對應(yīng)hash槽的元素會+1或不變,必不可能減少
// 只需要考慮是否要樹化
if (binCount != 0) {
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
// size加1荚虚,里面很復(fù)雜薛夜。。
addCount(1L, binCount);
return null;
}
決定是擴容還是樹化treeifyBin()
決定樹化會加鎖版述。
private final void treeifyBin(Node<K,V>[] tab, int index) {
Node<K,V> b; int n, sc;
if (tab != null) {
// 判斷數(shù)組長度是否小于64 梯澜,小于64就擴容數(shù)組
if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
tryPresize(n << 1);
// 數(shù)組已經(jīng)達到MIN_TREEIFY_CAPACITY,決定樹化
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
// 對數(shù)組對應(yīng)索引的根節(jié)點加鎖
synchronized (b) {
// 確保當(dāng)前節(jié)點仍然是第一個節(jié)點
if (tabAt(tab, index) == b) {
// 這里拷貝了一份鏈表渴析,并把節(jié)點換成TreeNode(left,right未初始化)
// 真正初始化操作在setTabAt
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;
}
// 樹化操作晚伙,賦值left right
setTabAt(tab, index, new TreeBin<K,V>(hd));
}
}
}
}
}
putTreeNode
final TreeNode<K,V> putTreeVal(int h, K k, V v) {
Class<?> kc = null;
boolean searched = false;
for (TreeNode<K,V> p = root;;) {
int dir, ph; K pk;
if (p == null) {
// 對應(yīng)位置沒有節(jié)點吮龄,直接放
first = root = new TreeNode<K,V>(h, k, v, null, null);
break;
}
else if ((ph = p.hash) > h)
// hash值比當(dāng)前節(jié)點小 之后找left節(jié)點
dir = -1;
else if (ph < h)
// hash值比當(dāng)前節(jié)點大 之后找right節(jié)點
dir = 1;
// 到這里代表hash值相等,到達目標位咆疗,返回當(dāng)前值
else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
return p;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&
(q = ch.findTreeNode(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.findTreeNode(h, k, kc)) != null))
return q;
}
dir = tieBreakOrder(k, pk);
}
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
TreeNode<K,V> x, f = first;
first = x = new TreeNode<K,V>(h, k, v, f, xp);
if (f != null)
f.prev = x;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
if (!xp.red)
x.red = true;
else {
lockRoot();
try {
root = balanceInsertion(root, x);
} finally {
unlockRoot();
}
}
break;
}
}
assert checkInvariants(root);
return null;
}
tryPresize()擴容數(shù)組
private final void tryPresize(int size) {
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
tableSizeFor(size + (size >>> 1) + 1);
int sc;
while ((sc = sizeCtl) >= 0) {
Node<K,V>[] tab = table; int n;
// 整個table尚未初始化漓帚,在putAll方法中,直接會調(diào)用tryPresize午磁,
// 所以在這里需要判斷tab是否已經(jīng)初始化
// 初始化后尝抖,把sizeCtl設(shè)置為0.75的數(shù)組長度
if (tab == null || (n = tab.length) == 0) {
n = (sc > c) ? sc : c;
if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
try {
if (table == tab) {
@SuppressWarnings("unchecked")
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
table = nt;
// 這里設(shè)置sizeCtl為0.75的size
sc = n - (n >>> 2);
}
} finally {
// 更新sizeCtl
sizeCtl = sc;
}
}
}
// 什么情況會到這里?已經(jīng)初始化過table迅皇,并且新增的元素數(shù)量并沒有超過0.75數(shù)組長度
else if (c <= sc || n >= MAXIMUM_CAPACITY)
break; // 此時c已經(jīng)過時牵署,或者c已經(jīng)超過最大值,無法再resize
else if (tab == table) {
int rs = resizeStamp(n);
/*
* 如果正在擴容Table的話喧半,則幫助擴容奴迅,支路①
* 否則的話,在支路②開始新的擴容
* 在transfer操作挺据,將第一個參數(shù)的table中的元素取具,移動到第二個元素的table中去,
* 雖然此時第二個參數(shù)設(shè)置的是null扁耐,但是暇检,在transfer方法中,當(dāng)?shù)诙€參數(shù)為null的時候婉称,
* 會創(chuàng)建一個兩倍大小的table
*/
// ①
if (sc < 0) {
Node<K,V>[] nt;
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
transferIndex <= 0)
break;
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
transfer(tab, nt);
}
// ②
else if (U.compareAndSwapInt(this, SIZECTL, sc,
(rs << RESIZE_STAMP_SHIFT) + 2))
transfer(tab, null);
}
}
}
擴容的核心方法transfer()
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
int n = tab.length, stride;
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
// 這里對stride有最小值限制块仆,避免占用太多CPU資源
stride = MIN_TRANSFER_STRIDE; // subdivide range
if (nextTab == null) { // initiating
try {
@SuppressWarnings("unchecked")
// 如果傳入的nextTab是null(tryPresize中存在這種情況)
// nextTab初始化為原tab長度的2倍
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) {
nextTable = null;
table = nextTab;
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)
advance = casTabAt(tab, i, null, fwd);
else if ((fh = f.hash) == MOVED)
advance = true; // already processed
else {
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;
}
}
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);
else
hn = new Node<K,V>(ph, pk, pv, hn);
}
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
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;
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) {
if ((p.prev = loTail) == null)
lo = p;
else
loTail.next = p;
loTail = p;
++lc;
}
else {
if ((p.prev = hiTail) == null)
hi = p;
else
hiTail.next = p;
hiTail = p;
++hc;
}
}
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;
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
setTabAt(tab, i, fwd);
advance = true;
}
}
}
}
}
}