ConcurrentHashMap 源码分析-扩容
扩容时的线程安全
ConcurrentHashMap 扩容的时机和 HashMap 相同,都是在 put 方法中的最后一步检查是否需要扩容。
ConcurrentHashMap 扩容的思路:
首先需要把老数组的值全部拷贝到扩容之后的新数组上,先从数组的队尾开始拷贝;
拷贝数组的槽点时,先把原数组槽点锁住,保证原数组槽点不能操作,成功拷贝到新数组时,把原数组槽点赋值为转移节点;
这时如果有新数据正好需要 put 到此槽点时,发现槽点为转移节点,就会一直等待,所以在扩容完成之前,该槽点对应的数据是不会发生变化的;
从数组的尾部拷贝到头部,每拷贝成功一次,就把原数组中的节点设置成转移节点;
直到所有数组数据都拷贝到新数组时,直接把新数组整个赋值给数组容器,拷贝完成。
源码:
方法主要分成了两步:
新建新的空数组
移动拷贝每个元素到新数组中去
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) {
…………
setTabAt(tab, i, fwd);
advance = true;
}
}
}
}
}
}
复制代码
Node<K,V>[] tab, Node<K,V>[] nextTab
tab:原数组,nextTab:新数组
int n = tab.length, stride;
获取老数组的长度
if (nextTab == null) { }
判断新数组为空,初始化,大小为原数组的两倍,n << 1
int nextn = nextTab.length;
获取新数组的长度
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
代表转移节点,如果原数组上是转移节点,说明该节点正在被扩容
for (int i = 0, bound = 0;;) { }
无限自旋,i 的值会从原数组的最大值开始,慢慢递减到 0
if (--i >= bound || finishing) advance = false;
结束循环的标志
if (i < 0 || i >= n || i + n >= nextn) { }
if 任意条件满足说明拷贝结束了
if (finishing) { nextTable = null; table = nextTab; sizeCtl = (n << 1) - (n >>> 1); return; }
拷贝结束,直接赋值,因为每次拷贝完一个节点,都在原数组上放转移节点,拷贝完成的节点数据一定不会发生变化。原数组发现是转移节点,是不会操作的,会一直等待转移节点消失之后在进行操作
if (tabAt(tab, i) == f) { }
进行节点的拷贝
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;
在老数组位置上放上 ForwardingNode 节点
总结:
拷贝槽点时,会把原数组的槽点锁住;
拷贝成功之后,会把原数组的槽点设置成转移节点,这样如果有数据需要 put 到该节点时,发现该槽点是转移节点,会一直等待,直到扩容成功之后,才能继续 put;
从尾到头进行拷贝,拷贝成功就把原数组的槽点设置成转移节点;等扩容拷贝都完成之后,直接把新数组的值赋值给数组容器,之前等待 put 的数据才能继续 put;
扩容通过在原数组上设置转移节点,put 时碰到转移节点时会等待扩容成功之后才能 put ,保证了整个扩容过程中肯定是线程安全的。
评论