ConcurrentHashMap源码解析(JDK 1.8)

时间:2021-03-14 17:18:20

一 成员变量解析

   
    /*
* Encodings for Node hash fields. See above for explanation.
*/
// 扩容节点的hash值
static final int MOVED = -1; // hash for forwarding nodes
// 树节点的hash值
static final int TREEBIN = -2; // hash for roots of trees
static final int RESERVED = -3; // hash for transient reservations
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash

/**
* map中元素的长度
*/
private transient volatile long baseCount;

/**
* table初始化以及扩容的控制符
*
* 负数代表正在进行初始化或扩容操作
* -1表示table正在初始化
* -n表示有 n-1个线程在执行扩容操作
* 正数或0代表hash表还没有被初始化,这个数值表示初始化或下一次进行扩容的大小
*/
private transient volatile int sizeCtl;

二 方法解析


1 put


(1)put方法

   
    final V putVal(K key, V value, boolean onlyIfAbsent) {
// ConcurrentHashMap不允许key或者value为null,这点和HashMap不同
if (key == null || value == null) throw new NullPointerException();
// 对key的hash进行二次hash,减少散列冲突
int hash = spread(key.hashCode());
int binCount = 0;
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
// 如果tab为空,则调用initTable()初始化tab
if (tab == null || (n = tab.length) == 0)
tab = initTable();
// tab[ (n - 1) & hash]为空节点,则直接创建一个新的节点
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
}
// 检查table[i]的节点的hash是否等于MOVED,如果等于MOVED,则检测到正在扩容,帮助其扩容
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else { // 节点f.hash不是MOVED
V oldVal = null;
// //锁定,(hash值相同的链表的头节点)
synchronized (f) {
// 避免多线程,需要重新检查
if (tabAt(tab, i) == f) {
if (fh >= 0) {
binCount = 1;
/*
*下面的代码就是先查找链表中是否出现了此key,如果出现,则更新value,并跳出循环,
*否则将节点加入到链表末尾并跳出循环
*/

for (Node<K,V> e = f;; ++binCount) {
K ek;
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;
}
}
}
}
// 如果链表长度已经达到临界值8,就需要把链表转换为树结构
if (binCount != 0) {
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
// 如果put是更新已有的key-value,那么返回旧value
if (oldVal != null)
return oldVal;
break;
}
}
}
// 将当前ConcurrentHashMap的元素数量+1
addCount(1L, binCount);
return null;
}

(2)ConcurrentHashMap的三个原子操作


    /**
* 获取tab在i位置上的节点
*/
static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
}

/**
* 利用CAS方法设置tab在i位置上的节点
* 将tab[i]和c作比较,相等则将tab[i]设置为v
* 否则不做修改
*/
static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
Node<K,V> c, Node<K,V> v) {
return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
}

/**
* 设置节点位置的值,仅在上锁区被调用
*/
static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
}

(3)初始化table函数

    下面是tab初始化函数
   
    private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
while ((tab = table) == null || tab.length == 0) {
// 如果sizeCtl为负数,则说明已经有其它线程正在进行扩容,即正在初始化或初始化完成,此时该线程放弃CPU调度
if ((sc = sizeCtl) < 0)
Thread.yield(); // lost initialization race; just spin
// 设置sizeCtl为-1,表示table正在初始化
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
try {
// 再次确认tab是否已经初始化
if ((tab = table) == null || tab.length == 0) {
// 设置n为DEFAULT_CAPACITY(16)
int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
@SuppressWarnings("unchecked")
/*
* 创建大小为n的node数组,并传给table
*/
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
table = tab = nt;
// sc=0.75n
sc = n - (n >>> 2);
}
} finally {
// sizeCtl = 0.75*Capacity,为扩容门限
sizeCtl = sc;
}
break;
}
}
return tab;
}

put()方法的大致流程如下:
    计算key两次哈希后的hash值
    if(tab为空)
        对tab进行初始化
               计算tab索引index = hash & (tab.size-1)
    else if(tab[index]为空)
        直接为tab[index]建立新节点
          else if(tab[index].hash == MOVE,即tab[index]正在进行扩容)
        调用helpTransfer(),帮助tab[index]扩容
    else
        if(tab[index].hash > 0)
            通过key和key的hash找到要更新的节点,若没有则在链表末尾添加新的node节点
        else if(tab[index]是树节点)
            在树种更新节点
    增加baseCount的值

2 get


(1)get方法


   
    public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode());
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
// 如果节点e的hash值小于0,说明该节点正在扩容,则调用节点的find()方法找寻
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}

(2)find

   
    /**
*Node类的find函数,通过遍历以该节点为首的链表来找寻匹配的值
*/
Node<K,V> find(int h, Object k) {
Node<K,V> e = this;
if (k != null) {
do {
K ek;
if (e.hash == h &&
((ek = e.key) == k || (ek != null && k.equals(ek))))
return e;
} while ((e = e.next) != null);
}
return null;
}

3 扩容


(1)ForwardingNode:一个特殊的Node节点,hash值为-1,其中存储nextTable的引用。只有table发生扩容的时候,ForwardingNode才会发挥作用,作为一个占位符放在table中表示当前节点为null或则已经被移动。


    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)扩容触发时机


1)如果新增节点之后,所在链表的元素个数达到了阈值 8 ,则会调用 treeifyBin 方法把链表转换成红黑树,不过在结构转换之前,会对数组长度进行判断。
如果数组长度n小于阈值 MIN_TREEIFY_CAPACITY ,默认是64,则会调用 tryPresize 方法把数组长度扩大到原来的两倍,并触发 transfer 方法,重新调整节点的位置。
2)put方法时,如果当前节点的hash为-1,则调用helpTransfer方法来触发扩容
3)addCount方法记录元素个数后检查是否需要进行扩容,当数组元素个数达到阈值时,会触发 transfer 方法,重新调整节点的位置。

(3)transfer

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 = MIN_TRANSFER_STRIDE; // subdivide range
if (nextTab == null) { // initiating
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) {
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;
}
}
}
}
}
}

transfer 方法实现了在并发的情况下,高效的从原始组数往新数组中移动元素,假设扩容之前节点的分布如下,这里区分蓝色节点和红色节点,是为了后续更好的分析:
ConcurrentHashMap源码解析(JDK 1.8)
在上图中,第14个槽位插入新节点之后,链表元素个数已经达到了8,且数组长度为16,优先通过扩容来缓解链表过长的问题,实现如下:

1)根据当前数组长度n来新建一个长度为2n的nextTable
       
        if (nextTab == null) {            // initiating
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;
}
2)初始化 ForwardingNode 节点,其中保存了新数组 nextTable 的引用,在处理完每个槽位的节点之后当做占位节点,表示该槽位已经处理过了
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
    
3)通过 for 自循环处理每个槽位中的链表元素,默认 advace 为真,通过CAS设置 transferIndex 属性值,并初始化 i 和 bound 值, i 指当前处理的槽位序号, bound 指需要处理的槽位边界,先处理槽位15的节点;
       
        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;
}
}
4)如果索引为15的节点为空,则通过CAS插入ForwardingNode 节点表示该节点已经被其他线程处理过
            else if ((f = tabAt(tab, i)) == null)
advance = casTabAt(tab, i, null, fwd);
    
5)如果索引为15的节点hash值等于-1,说明其他线程已经处理了该节点(该节点已经转换完毕),则直接跳过,处理索引为14的节点
            else if ((fh = f.hash) == MOVED)
advance = true; // already processed
          
6)处理槽位14的节点,是一个链表结构,先定义两个变量节点 ln 和 hn ,分别保存hash值的第X位为0和1的节点(X是由n来确定的,2^X=table.length),具体实现如下:
                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;
}

使用 fn&n 可以快速把链表中的元素区分成两类,A类是hash值的第X位为0,B类是hash值的第X位为1,并通过 lastRun 记录最后需要处理的节点,A类和B类节点可以分散到新数组的槽位14和30中,在原数组的槽位14中,蓝色节点第X为0,红色节点第X为1,把链表拉平显示如下:
ConcurrentHashMap源码解析(JDK 1.8)
1、通过遍历链表,记录 runBit 和 lastRun ,分别为1和节点6,所以设置 hn 为节点6, ln 为null;
2、重新遍历链表,以 lastRun 节点为终止条件,根据第X位的值分别构造ln链表和hn链表:

ln链:和原来链表相比,顺序已经不一样了

ConcurrentHashMap源码解析(JDK 1.8)


hn链:
ConcurrentHashMap源码解析(JDK 1.8)
通过CAS把ln链表设置到新数组的i位置,hn链表设置到i+n的位置;