1、ConcurrentHashmap的由來
HashMap不是線程安全的,在多線程情況下會導致擴容出現循環鏈表,造成訪問這個Entry的線程死循環,CPU接近100%;
Hashtable,使用synchronized進行線程安全的控制,因爲每次只有一個對象獲取監視器鎖,因此在高併發情況下,性能再次成爲瓶頸。類似表鎖;
ConcurrentHashmap1.6版本,採用分段鎖segment實現;segment繼承ReentrantLock作爲鎖角色,提供安全保障;segment包含若干個桶,每個桶由HashEntry構成鏈表;
ConcurrentHashmap1.8版本優化:①不再採用分段鎖,通過synchronized + CAS無鎖操作保證安全性;②數據結構採用:數組+鏈表+紅黑樹; 數據結構上採用鏈表 +紅黑樹是爲了保證穩定的查詢效率;
擯棄分段鎖原因? 降低鎖粒度,使得鎖粒度不會隨着擴容擴大併發度;
採用synchronized而不使用重入鎖的原因? synchronized鎖經過了偏向鎖 輕量級鎖 重量級鎖的優化,性能得到提升; 因爲synchronized是關鍵詞,而可重入鎖是實現類,在每個節點都通過重入鎖來獲取同步支持會帶來巨大的內存消耗;
總結: 無鎖-表鎖-分段鎖-行鎖
2-1、核心屬性
-
table volatile Node<K,V>[] table: //裝載Node的數組,作爲ConcurrentHashMap的數據容器,採用懶加載的方式,直到第一次插入數據的時候纔會進行初始化操作,數組的大小總是爲2的冪次方,默認初始大小爲16。
-
nextTable volatile Node<K,V>[] nextTable; //擴容時使用,平時爲null,只有在擴容的時候才爲非null
-
sizeCtl volatile int sizeCtl; 該屬性用來控制table數組的大小,根據是否初始化和是否正在擴容有幾種情況: ①當值爲負數時:1-1、如果爲-1表示正在初始化; 1-2、如果爲-N則表示當前正有N-1個線程進行擴容操作; ②當值爲正數時:2-1、如果當前數組爲null的話表示table在初始化過程中,sizeCtl表示爲需要新建數組的長度; 2-2、若已經初始化了,表示當前數據容器(table數組)可用容量也可以理解成臨界值(插入節點數超過了該臨界值就需要擴容),具體指爲數組的長度n 乘以 加載因子loadFactor; 2-3、當值爲0時,即數組長度爲默認初始值。
-
sun.misc.Unsafe U 在ConcurrentHashMapde的實現中可以看到大量的U.compareAndSwapXXXX的方法去修改ConcurrentHashMap的一些屬性。這些方法實際上是利用了CAS算法保證了線程安全性,這是一種樂觀策略,假設每一次操作都不會產生衝突,當且僅當衝突發生的時候再去嘗試。而CAS操作依賴於現代處理器指令集,通過底層CMPXCHG指令實現。CAS(V,O,N)核心思想爲:若當前變量實際值V與期望的舊值O相同,則表明該變量沒被其他線程進行修改,因此可以安全的將新值N賦值給變量;若當前變量實際值V與期望的舊值O不相同,則表明該變量已經被其他線程做了處理,此時將新值N賦給變量操作就是不安全的,在進行重試。而在大量的同步組件和併發容器的實現中使用CAS是通過
sun.misc.Unsafe
類實現的,該類提供了一些可以直接操控內存和線程的底層操作;
2-2、關鍵內部類
-
Node Node類實現了Map.Entry接口,主要存放key-value對,並且具有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; ...... }
另外可以看出很多屬性都是用volatile進行修飾的,也就是爲了保證內存可見性。
2.TreeNode 樹節點,繼承於承載數據的Node類。而紅黑樹的操作是針對TreeBin類的,從該類的註釋也可以看出,也就是TreeBin會將TreeNode進行再一次封裝
**
* Nodes for use in TreeBins
*/
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;
......
}
3.TreeBin 這個類並不負責包裝用戶的key、value信息,而是包裝的很多TreeNode節點。實際的ConcurrentHashMap“數組”中,存放的是TreeBin對象,而不是TreeNode對象。
static final class TreeBin<K,V> extends Node<K,V> {
TreeNode<K,V> root;
volatile TreeNode<K,V> first;
volatile Thread waiter;
volatile int lockState;
// values for lockState
static final int WRITER = 1; // set while holding write lock
static final int WAITER = 2; // set when waiting for write lock
static final int READER = 4; // increment value for setting read lock
......
}
4.ForwardingNode 在擴容時纔會出現的特殊節點,其key,value,hash全部爲null。並擁有nextTable指針引用新的table數組。
static final class ForwardingNode<K,V> extends Node<K,V> {
final Node<K,V>[] nextTable;
ForwardingNode(Node<K,V>[] tab) {
super(MOVED, null, null, null);
this.nextTable = tab;
}
.....
}
2-3、CAS操作
1、tabAt(Node<K,V>[] tab, int i) 獲取table數組中索引爲i的Node元素
2、casTabAt(Node<K,V>[] tab, int i, Node<K,V> c, Node<K,V> v)利用CAS操作設置table數組中索引爲i的元素
3、setTabAt(Node<K,V>[] tab, int i, Node<K,V> v)設置table數組中索引爲i的元素
3-1、源碼分析:put
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode()); //key取hash值;
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(); //@1 第一次put:table不存在則初始化
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) { //(n - 1) & hash利用N爲2的n次方進行巧妙的與取模;
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // 第一次put新構造的Node節點到桶,直接put,成功後直接跳出循環;
}
else if ((fh = f.hash) == MOVED) //@2 當前Entry的hash值爲MOVED立即幫助擴容;當併發越高的時候也可以實現更快速的擴容;充分利用併發性;
tab = helpTransfer(tab, f);
else {
V oldVal = null;
synchronized (f) { //鎖住當前的Entry
if (tabAt(tab, i) == f) {
if (fh >= 0) { //當前爲鏈表,在鏈表中插入新的鍵值對
binCount = 1; //鏈表長度
for (Node<K,V> e = f;; ++binCount) {
K ek;
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) { //當元素key值相同則覆蓋值
oldVal = e.val;
if (!onlyIfAbsent)
e.val = value;
break;
}
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); //@ 3當實際大小超過TREEIFY_THRESHOLD-8 轉換爲紅黑樹
if (oldVal != null)
return oldVal;
break;
}
}
}
addCount(1L, binCount); // @4 put成功則計算器增加1;這裏有優秀的設計模式;判斷是否需要擴容
return null;
}
//@1 第一次put:table不存在則初始化 initTable();
private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
while ((tab = table) == null || tab.length == 0) {
if ((sc = sizeCtl) < 0) //sizeCtl < 0 保證只有一個線程正在進行初始化; sizeCtl = 0 數組長度爲默認初始值; sizeCtl > 0 爲數組長度
Thread.yield(); // lost initialization race; just spin
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { //開始初始化設置 SIZECTL = -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 = n - (n >>> 2); //設置sizeCtl 爲擴容臨界值
}
} finally {
sizeCtl = sc;
}
break;
}
}
return tab;
}
//@2 當前Entry的hash值爲MOVED立即幫助擴容 tab = helpTransfer(tab, f);
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) { //nextTable指向新的引用數組
int rs = resizeStamp(tab.length); //加工標記位,與擴容前的數組長度相關
while (nextTab == nextTable && table == tab &&
(sc = sizeCtl) < 0) {
//sizeCtl 1000 0000 0001 1011 0000 0000 0000 0010 高16位爲標記位,低16位爲擴容線程數;設計目的:確保併發擴容,擴容戳唯一;
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 || //擴容標記相等 || 預定線程數爲1 || 線程數已經達到最大容量 || 擴容下標爲0-擴容已完成
sc == rs + MAX_RESIZERS || transferIndex <= 0) //則不進行擴容
break;
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
transfer(tab, nextTab); // @5 重點:擴容邏輯;
break;
}
}
return nextTab;
}
return table;
}
//1左移15位, | 無符號整型的最高非零位前面0的個數 ;
//16 = 0000 0000 0000 0000 0000 0000 0001 0000;
// Integer.numberOfLeadingZeros(16) = 27 = 0001 1011
// 1 << (RESIZE_STAMP_BITS - 1) = 0000 0000 0000 0000 1000 0000 0000 0000
// 結果:0000 0000 0000 0000 1000 0000 0001 1011
static final int resizeStamp(int n) {
return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
}
//@ 3當實際大小超過TREEIFY_THRESHOLD 轉換爲紅黑樹 treeifyBin(tab, i);
// 紅黑樹簡單介紹: 紅黑樹4條原則; 本身是一個平衡二叉樹,確保到任何一個葉節點查詢效率一致 ; 平衡方法:通過左旋、右旋實現;
// @4 put成功則計算器增加1;這裏有優秀的設計模式;判斷是否需要擴容 addCount(1L, binCount);
private final void addCount(long x, int check) {
CounterCell[] as; long b, s;
if ((as = counterCells) != null ||
!U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) { //對 baseCount做原子遞增,僅嘗試一次,失敗則放棄;
/**
*在高併發下如何高效安全的實現存儲數據個數的統計?
*設計思想:分而治之; 加鎖會導致性能下降,case在高併發情況下會循環cas造成性能下降;
* 用 CounterCell[] 去分別記錄元素的個數,分片處理,降低併發;併發加大時也可以增加數組長度;
* sumCount() 彙總元素個數;baseCount:元素基本個數;
*/
CounterCell a; long v; int m;
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 ||
(a = as[ThreadLocalRandom.getProbe() & m]) == null || // 生成一個線程安全的隨機數(線程安全的隨機數);
!(uncontended =
U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
fullAddCount(x, uncontended); //增加計數:首先初始化當前的CounterCell[],然後噹噹前節點爲null則初始化節點CounterCell;然後存在CounterCell 時則CAS累加;
return;
}
if (check <= 1)
return;
s = sumCount();
}
if (check >= 0) {
Node<K,V>[] tab, nt; int n, sc;
while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
(n = tab.length) < MAXIMUM_CAPACITY) { //擴容:大於擴容因子 && tab存在 && tab長度 < 最大容量
int rs = resizeStamp(n);
if (sc < 0) {
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);
s = sumCount();
}
}
}
// @5 重點:擴容邏輯: 1,擴大數組長度 2,轉移原本的數據鏈-進行數據遷移
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 = MIN_TRANSFER_STRIDE; // 細分:每個cpu至少處理16個Entry,的擴容遷移,cpu爲1則全部由該cpu處理;
if (nextTab == null) { // initiating
try {
@SuppressWarnings("unchecked")
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1]; //2倍擴容
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); //nextTab爲新table
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 //第一個進程進來:i=31 bound = 16是其處理範圍的槽點;第二個線程進來爲 i= 15 bound = 0;
(this, TRANSFERINDEX, nextIndex,
nextBound = (nextIndex > stride ?
nextIndex - stride : 0))) {
bound = nextBound;
i = nextIndex - 1;
advance = false;
}
}
if (i < 0 || i >= n || i + n >= nextn) { //當前線程如果處理完成則對擴容線程數-1,釋放調用線程;
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); //已經處理過了,是一個null節點
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; //當前節點的hash值 & n(數組長度),得到當前鏈表是高位1 還是低位0
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); 高位鏈遷移到 + n 的位置
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;
}
}
}
}
}
}
3-2、源碼分析:get
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
// 1. 重hash
int h = spread(key.hashCode());
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
// 2. table[i]桶節點的key與查找的key相同,則直接返回
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
// 3. 當前節點hash小於0說明爲樹節點,在紅黑樹中查找即可
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
while ((e = e.next) != null) {
//4. 從鏈表中查找,查找到則返回該節點的value,否則就返回null即可
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}