//以下皆为个人学习记录,可能不是很准确,欢迎评论区纠正
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
//计算key哈希值
int hash = spread(key.hashCode());
int binCount = 0;//表示链表长度
//不停循环直到遇到break或者return
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
//如果数组为空,尝试初始化数组
if (tab == null || (n = tab.length) == 0)
tab = initTable();
//如果hash对应的数组位置为空,直接使用cas将key,value放入
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
}
//如果hash对应的节点为正在移动或者已经移动完毕的节点,说明此时数组正在扩容,则尝试帮助扩容
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
//数组中hash对应的位置有节点,可能是链表节点或者是红黑树节点
else {
V oldVal = null;
//直接对节点上锁
synchronized (f) {
//重新检查节点是否还在对应位置
if (tabAt(tab, i) == f) {
//节点hash大于0一定是链表节点(链表节点数可能多个也可能单个)(红黑树顶上的节点TreeBin的hash为-2,move为-1)
if (fh >= 0) {
binCount = 1;
for (Node<K,V> e = f;; ++binCount) {
K ek;
//存在相同的key
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
oldVal = e.val;
//存在相同的key,onlyIfAbsent为false,就覆盖
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
//不是相同的key,新节点就插入在链表后面
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
//如果是红黑树的头部节点,就把key,value封装进红黑树里(TreeBin是红黑树的头部节点,不存储key,value)
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) {
//如果链表节点数量大于9,就尝试去扩容或者转链表为红黑树
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
//如果有修改前的旧value,就返回
if (oldVal != null)
return oldVal;
break;
}
}
}
//map的size+1
addCount(1L, binCount);
return null;
}
//替换给定索引处bin中的所有链接节点,除非表太小,否则会调整大小。
/**
* 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) {
//如果数组长度小于最小树化尺寸,则不转红黑树,改成扩容
if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
tryPresize(n << 1);
//转红黑树
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
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;
}
setTabAt(tab, index, new TreeBin<K,V>(hd));
}
}
}
}
}
//尝试预先设置表的大小以容纳给定数量的元素。
/**
* Tries to presize table to accommodate the given number of elements.
*
* @param size number of elements (doesn't need to be perfectly accurate)
*/
private final void tryPresize(int size) {
//大于最大容量MAXIMUM_CAPACITY,则扩容到MAXIMUM_CAPACITY,否则扩容为原来的2倍
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
//tableSizeFor返回数为2的倍数
tableSizeFor(size + (size >>> 1) + 1);
int sc;
while ((sc = sizeCtl) >= 0) {
Node<K,V>[] tab = table; int n;
//如果map未初始化则进行初始化数组,控制sizeCtl为数组长度的0.75倍
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;
sc = n - (n >>> 2);
}
} finally {
sizeCtl = sc;
}
}
}
//大于最大容量则不扩容(我也不懂c <= sc 是什么意思)
else if (c <= sc || n >= MAXIMUM_CAPACITY)
break;
else if (tab == table) {
//rs第16位一定是1,低位是数组长度的标识
int rs = resizeStamp(n);
//此时数组已经初始化,如果sc<0,说明正在扩容
if (sc < 0) {
Node<K,V>[] nt;
// 如果数组正在扩容,那sc因该等于(rs << RESIZE_STAMP_SHIFT) + 2),则sc >>> RESIZE_STAMP_SHIFT) == rs,若不相等,说明标识不同,可能数组已经扩容完毕或者进入下一轮扩容了,此时不能参与协助扩容
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
transferIndex <= 0)
break;
//此时尝试去协助扩容, sc + 1表示多一个线程协助扩容
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);
}
}
}
//移动和或将每个bin中的节点复制到新表中。请参阅上面的说明
/**
* 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;
//转移区间的大小设置,如果为单核心,一次转移数组长度的区间,如果为多核心,则区间为数组长度除以8除以cpu个数
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;
//根据stride区间大小,计算出i和bound作为数组迁移开始位置和结束迁移位置,方便后续对这个区间进行迁移
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;
}
}
//如果i<0说明扩容结束了就不要继续往下走了
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
}
}
//如果这个位置没有节点直接用cas放一个ForwardingNode节点在上面
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;
//fh>0说明是链表节点
if (fh >= 0) {
int runBit = fh & n;
Node<K,V> lastRun = f;
//找出最后一个不同的节点,则该节点的后面的节点的hash一定和这个节点相同,
//到时候直接移动该节点到新数组,该节点后的节点会自动移动到新数组去
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;
// 节点hash ph和 n与算法,如果为0说明节点在新数组的位置和旧数据相同,即为i,
if ((ph & n) == 0)
ln = new Node<K,V>(ph, pk, pv, ln);
//如果不为0说明移动前后位置不同,需要偏移下,即为i+n
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;
}
}
}
}
}
}
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code key.equals(k)},
* then this method returns {@code v}; otherwise it returns
* {@code null}. (There can be at most one such mapping.)
*
* @throws NullPointerException if the specified key is null
*/
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode());
//hash命中,并且对应节点不是null
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;
}
//如果是红黑树
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;
}
//计算总数
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)) {
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);
return;
}
if (check <= 1)
return;
s = sumCount();
}
if (check >= 0) {
Node<K,V>[] tab, nt; int n, sc;
//如果s大于阈值,也可能会出发扩容
while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
(n = tab.length) < MAXIMUM_CAPACITY) {
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();
}
}
}
ConcurrentHashMap扩容时机
1.put数据时,如果链表长度超过8,转红黑树之前,会先检查是否需要扩容,没到阈值都可以直接扩容
2.put数据后,检查总的node数量是否超过阈值(数组长度的0.75),超过的话扩容
ConcurrentHashMap的锁机制
put的时候,如果数组上对应位置没有节点,则cas乐观锁进行存值
如果数组对应位置有节点,则synchronized锁住对应节点
get方法中是否存在锁?
get方法中没有cas锁,也没有synchronized锁住对应节点,但是会受到红黑树中的写锁影响,因为红黑树的平衡过程会影响读取数据
ConcurrentHashMap各种数值?
阈值=数组容量*0.75
数组默认长度16,因此阈值默认12
扩容都是数组直接变成2倍容量
jdk1.8 ConcurrentHashMap结构?
数组+链表+红黑树
ConcurrentHashMap扰动算法?
(n - 1) & ( (key.hashCode() ^ (key.hashCode() >>> 16)) & 0x7fffffff)
即key进行hash算出hashcode,然后低位和高位做异或运算,取正数,和数组长度-1进行与运算。
数组长度-1的目的是为了有更多数字1,毕竟数组长度都是2的整数幂。
& 0x7fffffff即为取正数。
>>> 16是为了得到高位
异或运算是为了让高位参与进数组定位中,即增加扰动,让位置更加分散
sizeCtl的含意义?
sizeCtl是用于控制数组size的一个int变量,常常和cas配合使用
1.为正数,ConcurrentHashMap初始化完成正在使用,置为size * 0.75
2.如果一个ConcurrentHashMap正在初始化,值为-1
3.如果为0.表示未初始化
4.为负数并且不为-1表示在扩容
rs << RESIZE_STAMP_SHIFT) + 2 高16位是扩容标识戳,低16位是扩容线程数+1
更多资料请B站搜索up 李哈zzz, 这个讲师很棒,里面有逐句分析源码教程。