前言
本文针对 JDK 1.8 的 ConcurrentHashMap 进行总结。
建议没读过源码的可以认真过一下,看过源码可以直接跳总结。
属性
// 桶数组
transient volatile Node[] table;
// 扩容数组,只会在扩容中不会为空
private transient volatile Node[] nextTable;
// 控制扩容的变量。当为负数的时候,table正在初始化或者扩容,-1表示正在初始化,-n表示正在进行扩容的线程数。当table为null时,变量为创建table的大小或者0值。当table初始化完成,变量为下一次扩容的大小
private transient volatile int sizeCtl;
/* ---------------- Constants -------------- */
// table的最大容量。2的30次方=1073741824
private static final int MAXIMUM_CAPACITY = 1 << 30;
// table的初始默认容量。
private static final int DEFAULT_CAPACITY = 16;
// 最大的数组容量。2^31-1=2147483647
static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
// 默认的并发等级。能够同时更新ConccurentHashMap且不产生锁竞争的最大线程数,在Java8之前实际上就是ConcurrentHashMap中的分段锁个数,即Segment[]的数组长度。正确地估计很重要,当低估,数据结构将根据额外的竞争,从而导致线程试图写入当前锁定的段时阻塞;相反,如果高估了并发级别,你遇到过大的膨胀,由于段的不必要的数量; 这种膨胀可能会导致性能下降,由于高数缓存未命中。在Java8里,仅仅是为了兼容旧版本而保留。唯一的作用就是保证构造map时初始容量不小于concurrencyLevel。
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
// 进行扩容的负载因子
private static final float LOAD_FACTOR = 0.75f;
// 链表树化的阈值
static final int TREEIFY_THRESHOLD = 8;
// 树链表化的阈值
static final int UNTREEIFY_THRESHOLD = 6;
// 红黑树的最小容量。最小为4倍的TREEIFY_THRESHOLD
static final int MIN_TREEIFY_CAPACITY = 64;
/**
* 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;
/**
* The number of bits used for generation stamp in sizeCtl.
* Must be at least 6 for 32bit arrays.
*/
private static int RESIZE_STAMP_BITS = 16;
static final int MOVED = -1; // hash for forwarding nodes
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
// CPU的核心数
static final int NCPU = Runtime.getRuntime().availableProcessors();
put
- 计算hash(key的hashCode高16位与低16位异或,然后根据容量取模)
- 遍历table,插入:
- table为空,初始化
- 桶为空,插入
- 若发现table正在扩容,则帮忙扩容
- 若桶不为空,把链头/树根锁住,插入
- 发现桶里链表的节点数大于树化的阈值并且桶个数达到64
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
// 将key的hashcode进行一个转换
int hash = spread(key.hashCode());
int binCount = 0;
// 遍历整个table。这里是个死循环,直至插入成功
for (Node[] tab = table;;) {
// f 当前的桶;n 桶的总长度;i 桶所在的位置;fh 桶中元素的hash
Node f; int n, i, fh;
// 若表是空的,则初始化
if (tab == null || (n = tab.length) == 0)
tab = initTable();
// 若表不为空。则判断表中应该插入的位置是否为空,插入的位置通过 变换后的hash值 与 表长度取模之后求得
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
// cas操作插入该值
if (casTabAt(tab, i, null,
new Node(hash, key, value, null)))
break; // no lock when adding to empty bin
}
// 若监测到其他地方在扩容,则会协助扩容
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
// 同步代码块,保证线程安全
synchronized (f) {
if (tabAt(tab, i) == f) {
// 若fh零,则桶中的数据结构为链表,则只需要找到插入的位置即可
if (fh >= 0) {
binCount = 1;
for (Node 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 pred = e;
if ((e = e.next) == null) {
pred.next = new Node(hash, key,value, null);
break;
}
}
}
// 若为TreeBin,则fh为 -1。且判断f的类类型为TreeBin,则插入到树的结构中
else if (f instanceof TreeBin) {
Node p;
binCount = 2;
if ((p = ((TreeBin)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;
}
}
}
addCount(1L, binCount);
return null;
}
get
public V get(Object key) {
Node[] tab; Node e, p; int n, eh; K ek;
int h = spread(key.hashCode());
// 若table不为空,且索引所对应的bucket不为空
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
// 头结点的hash与要找的hash相等,且提取出的key相同,则将值返回
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
// bucket所对应的数据结构为树(TreeBin的hash为-2)
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
// bucket所对应的数据结构为链表
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
replaceNode
- 计算hash
- 遍历table
- table为空或索引为空,break
- table正在扩容就帮他
- 链表 -> 锁头 -> 删除
- 红黑树 -> 锁根 -> 删除 -> 判断是否需要退化成链表
// key;value:新值;cv:旧值
final V replaceNode(Object key, V value, Object cv) {
// 转换hash
int hash = spread(key.hashCode());
// 遍历整个table,直到替换完成
for (Node[] tab = table;;) {
Node f; int n, i, fh;
// 若这个需要替换的节点不存在,break
if (tab == null || (n = tab.length) == 0 ||
(f = tabAt(tab, i = (n - 1) & hash)) == null)
break;
// 若正在扩容,则先帮组扩容
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
// 操作是否有效的标志
boolean validated = false;
// 加锁
synchronized (f) {
if (tabAt(tab, i) == f) {
// 链表
if (fh >= 0) {
validated = true;
for (Node e = f, pred = null;;) {
K ek;
// 找到hash相等,且key相等的位置
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
V ev = e.val;
// 若新值为空 || 新值等于旧值 || 若旧值不为空,且旧值等于找到的值
if (cv == null || cv == ev ||
(ev != null && cv.equals(ev))) {
oldVal = ev;
// 若新值不为空,则替换成新值。对应的replace方法
if (value != null)
e.val = value;
// 若新值为空,且前继节点不为空,则改变指针的指向。对应的remove方法
else if (pred != null)
pred.next = e.next;
// 若前继节点为空,当前remove的是头节点,则需要改变头,即bin
else
setTabAt(tab, i, e.next);
}
break;
}
pred = e;
if ((e = e.next) == null)
break;
}
}
// 红黑树
else if (f instanceof TreeBin) {
validated = true;
TreeBin t = (TreeBin)f;
TreeNode r, p;
// 若树的根节点不为空,且能够找到该节点
if ((r = t.root) != null &&
(p = r.findTreeNode(hash, key, null)) != null) {
V pv = p.val;
if (cv == null || cv == pv ||
(pv != null && cv.equals(pv))) {
oldVal = pv;
// 替换
if (value != null)
p.val = value;
// 删除。若返回true,则表示现在的树size很小,需要将树变成链表
else if (t.removeTreeNode(p))
setTabAt(tab, i, untreeify(t.first));
}
}
}
}
}
// 若操作有效
if (validated) {
if (oldVal != null) {
if (value == null)// 若不为空,新值为空,代表为删除操作,count需要减一
addCount(-1L, -1);
return oldVal;
}
break;
}
}
}
return null;
}
transfer
private final void transfer(Node[] tab, Node[] nextTab) {
int n = tab.length, stride;
// 为每个内核均分任务,并保证其不小于16
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
stride = MIN_TRANSFER_STRIDE; // subdivide range
// 若nextTab为null,则初始化其为原table的2倍;
if (nextTab == null) { // initiating
try {
@SuppressWarnings("unchecked")
Node[] nt = (Node[])new Node[n << 1];
nextTab = nt;
} catch (Throwable ex) { // try to cope with OOME
sizeCtl = Integer.MAX_VALUE;
return;
}
nextTable = nextTab;
// 首先进行转移的 bucket 索引位置
transferIndex = n;
}
int nextn = nextTab.length;
// 转发节点
ForwardingNode fwd = new ForwardingNode(nextTab);
boolean advance = true;
boolean finishing = false; // to ensure sweep before committing nextTab
for (int i = 0, bound = 0;;) {
Node f; int fh;
// 初始化转换的bucket的index,和该线程处理的边界
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;
}
}
// bucket的索引位置超过了边界,扩容完成
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
}
}
// 若当前bucket为空,则没有要转移的链表
else if ((f = tabAt(tab, i)) == null)
advance = casTabAt(tab, i, null, fwd);
// 若当前bucket的hash为MOVED状态,说明已经处理过了
else if ((fh = f.hash) == MOVED)
advance = true; // already processed
else {
// 进行转移的处理,需要同步,加锁
synchronized (f) {
if (tabAt(tab, i) == f) {
// ln低位的头节点,hn高位的头节点
// 因为table扩容两倍,相当于增加了一位。所以在rehash的过程中,对于某个bucket只需要计算增加的那一位mod之后是0,还是1.
// 若为0,则保持位置 i 不动。若为1,则需要转移到 i + (增加size)的位置上
Node ln, hn;
// 若头节点的hash大于0,说明为链表,做链表处理操作
if (fh >= 0) {
int runBit = fh & n;
Node lastRun = f;
for (Node 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 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(ph, pk, pv, ln);
else
hn = new Node(ph, pk, pv, hn);
}
// 将链表设置到对应的bucket上
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
setTabAt(tab, i, fwd);
advance = true;
}
// 处理红黑树
else if (f instanceof TreeBin) {
TreeBin t = (TreeBin)f;
// lo和hi分别为bucket地位和高位的链表头结点
TreeNode lo = null, loTail = null;
TreeNode hi = null, hiTail = null;
int lc = 0, hc = 0;
// 遍历整个树,将所有的节点根据mod最高位是否为1,分别加入到lo和hi的链表中
for (Node e = t.first; e != null; e = e.next) {
int h = e.hash;
TreeNode p = new TreeNode
(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(lo) : t;
hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
(lc != 0) ? new TreeBin(hi) : t;
// 分别设置到对应的tab中
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
setTabAt(tab, i, fwd);
advance = true;
}
}
}
}
}
}
count
private final void addCount(long x, int check) {
CounterCell[] as; long b, s;
// 若 counterCells 不为 null, 则使用 counterCells 计数,因为已经发生并发情况了
// 若 counterCells 为 null,CAS 更新 baseCount 失败(说明并发情况,有线程更新失败),则用 counterCells 计数
if ((as = counterCells) != null ||
!U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
CounterCell a; long v; int m;
boolean uncontended = true;
// 若 as 为 null 则需要初始化这个数组
// 若 as 不为 null,根据规则找到数组对应的 index,然后 cas 更新这个技术
// 若出现 cas 失败,则是对这个 段 有多个线程竞争,则需要循环使更新成功,这些工作在 fullAddCount 方法中执行
// 当 counterCells 变得不稳定的时候,也会进行扩容
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) {
// ...
// 里面判断是否需要扩容 hashtable
}
}
总结
- 实现
对于 1.7 是 一个 Segment 数组和多个 HashEntry 组成,并发锁 Segment
对于 1.8 是 (volatile)Node数组 + 链表 + 红黑树 ,并发使用 Synchronized 和 CAS
- size
JDK1.7 和 JDK1.8 对 size 的计算是不一样的。
1.7 中是先不加锁计算三次,如果三次结果不一样在加锁。
1.8 size 是通过对 baseCount 和 counterCell 进行 CAS 计算,最终通过 baseCount 和 遍历 CounterCell 数组得出 size。
1.8 推荐使用mappingCount 方法,因为这个方法的返回值是 long 类型,不会因为 size 方法是 int 类型限制最大值。
- 初始化
Node 的 val 和 next 在扩容时候会发生变化,所以需要 volatile 来保证可见性和禁止指令重排,解除伪共享。
- put
CAS 尝试自旋写入,自旋失败再用 Synchronized 锁住再写入。
值得一提的是ConcurrentHashMap 是不能存入 Key / Value 为 null 的元素。
- get
CAS 保证变量的原子性。