1、put方法源码
public V put(K key, V value) {
return putVal(key, value, false);
}
2、在put方法中,调用了putVal方法。由于该方法代码较多,我们只保留框架性质的代码,这样会更方便我们理解。如下所示:
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {
... ...
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
if (tab == null || (n = tab.length) == 0)
tab = initTable();
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
}
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
synchronized (f) {
... 针对f链表或红黑树进行添加Node节点操作,执行完毕后break ...
}
if (binCount != 0) {
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
addCount(1L, binCount);
return null;
}
3、上面的代码,可以分为两部分内容,
(1)、第一部分:首先开启了无限循环,在里面进行了4中情况的判断。
(2)、 第二部分:执行addCount,将ConcurrentHashMap中存储的k,v总数+1。
private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
//table=null
while ((tab = table) == null || tab.length == 0) {
//sizeCtl =0
//如果sizeCtl为-1,则表示table数组正在被别的线程初始化
if ((sc = sizeCtl) < 0)
/** * 其他竞争失败的线程,会在while循环中spin,直到table创建完毕 * 才能跳出while循环 */
Thread.yield();
/** * 如果我们是第一个执行初始化table数组,那么我们首先通过CAS把 * sizeCtl设置为-1表明名正在初始化中。 * * 只有一条线程可以成功执行CAS操作,将 sizeCtl赋值为-1,其他竞争失败 * 的线程,在外面spin,直到table创建完毕才能跳出while循环。 */
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
try {
//table=null
if ((tab = table) == null || tab.length == 0) {
//sc =0 ,DEFAULT_CAPACITY=16
int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
@SuppressWarnings("unchecked")
// n =16
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
table = tab = nt;
//n>>>2=10000>>>2=00100=4 (16/2/2=4)
/**sc= (4-1)/4n =0.75*n*/
sc = n - (n >>> 2); //sc =16-4 =12
}
} finally {
//sc=12, sizeCtl =12
sizeCtl = sc;
}
break;
}
}
//返回size为16的空的Node数组
return tab;
}
while ((tab = table) == null || tab.length == 0)
private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
while ((tab = table) == null || tab.length == 0) {
if ((sc = sizeCtl) < 0) {
Thread.yield(); // 其他竞争失败的线程,会在while循环中spin,直到table创建完毕才能跳出while循环
}
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
/** * 只有一个线程可以成功执行CAS操作,将sizeCtl赋值为-1 */
... 此处代码省略 ...
/** * 执行创建table数组操作 */
... 此处代码省略 ...
}
}
return tab; // 返回创建好的table数组
}
/** *目标位置为空,直接设置 *通过hash值对应的数组下标得到第一个节点;虽然table数组是线程间可见,但是数组元素未必。 *所以,以volatile读的方式来读取table数组中的元素,保证每次拿到的数组都是最新的 *i=[在数组中的位置] *tabAt(tab, i = (n - 1) & hash)相当于tab[i] */
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {//f =null i=[在数组中的位置]
//如果该下标返回的节点为空,则直接通过cas将新的值封装成node插入即可;
//如果cas失败,说明存在竞争,则进入下一次循环
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null))){
break;
}
}
private static final long ABASE;
static {
try {
... 此处代码省略...
Class<?> ak = Node[].class;
/** *数组对象:对象头(8字节)* 指针(4字节)* 数组长度(4字节)=16,所以 *基础偏移量为16 * * public native int arrayBaseOffset(Class<?> var1); * 返回数组类型的[第一个元素]的偏移地址(基础偏移地址)。 * 如果arrayIndexScale方法返回的比例因子不为0,可以结合基础偏移地址 * 和比例因子访问数组的所有元素。 * Unsafe类中已经初始化了很多类似的常量如ARRAY_BOOLEAN_BASE_OFFSET等。 */
ABASE = U.arrayBaseOffset(ak);//16
/** * public native int arrayIndexScale(Class<?> var1); * 返回数组类型的[比例因子](其实就是【数组中元素偏移地址的增量】, * 因为数组中的元素的地址是连续的)。 * 此方法不适用于数组类型为“narrow”类型的数组,“narrow”类型的数组类型 * 使用此方法会返回0(这里narrow应该是狭义的意思,但是具体指哪些类型 * 暂时不明确)。 * Unsafe类中已经初始化了很多类似的常量如ARRAY_BOOLEAN_INDEX_SCALE等。 **/
int scale = U.arrayIndexScale(ak);//4
if ((scale & (scale - 1)) != 0)// 4 & (4-1) =0100 & 0011 =0000
throw new Error("data type scale not a power of two");
/** *Integer.numberOfLeadingZeros(int i) *给定一个int类型数据,返回这个数据的二进制串中从最左边算起连续的“0”的 *总数量。 *而ASHIFT其实就是将scale数值转换为[按位左移对应的数值], *即:通过scale =4,name计算出ASHIFT=2, *而N<<2其实就相当于N*2*2=N*4=N*scale *举例: * 如果scale =8(十进制)=1000(二进制) * name计算出Integer.numberOfLeadingZeros(scale)=28 * ASHIFT =31-28 =3 * N<<3 =n*2*2*2=N*8=N*scale */
//ASHIFT=31-29=2
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
} catch (Exception e) {
throw new Error(e);
}
}
if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value, null))) {
break;
}
/** *插入元素遇到扩容,即MOVED =-1,表示插入元素时,无限循环内部正好在扩容;那么就 *调用helpTransfer(tab, f)一起扩容; *由于是在for(Node<K,V>[] tab =table;;),所以当帮忙扩容完毕后,还会像新的 *table中插入元素的。 */
else if ((fh = f.hash) == MOVED){
tab = helpTransfer(tab, f);
}
我们调用put方法的时候,此部分为最核心的处理逻辑了。因为当table表被初始化了,并且出现哈希冲突了,并且Node 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)))) {
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;
}
}
}
如果Node链表长度大于等于9,则执行treeifyBin方法进行扩容或者转换元素存储操作。
首先,通过tableSizeFor根据size计算出2的n次方所有值中,所有大于size值中最小的那个值。具体的逻辑,参考【6.4.4> tableSizeFor(int c)】
if (tab == null || (n = tab.length) == 0) {
n = (sc > c) ? sc : c;
/** 将sizeCtl设置为-1 */
if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
try {
if (table == tab) {
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
table = nt;
sc = n - (n >>> 2); /** 3/4*n */
}
} finally {
/** 将sizeCtl设置为3/4的table数组长度 */
sizeCtl = sc;
}
}
}
)
1、源码如下
2、resizeStamp方法的具体作用是返回table数组长度相关信息。源码如下所见:
3、resizeStamp方法的解释
4、我们介绍完resizeStamp方法后,往下看if判断,sc表示sizeCtl,如果sc < 0,则说明table数组正在被其他线程操作着(比如:扩容)。
if (sc < 0) {
Node<K,V>[] nt;
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);
}
1、源码如下所示:
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE) {
stride = MIN_TRANSFER_STRIDE;
}
2、扩容时,计算每次转移的固定节点数(步长)
3、我们假设现在有A、B两个线程共同执行transfer,入参tab的长度为32,nextTable=null。
if (nextTab == null) { // eg1: nextTab=null
try {
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1]; /** nt=2*n */
nextTab = nt; /** 由于nextTab为null,所以此处初始化一个nextTab */
} catch (Throwable ex) {
sizeCtl = Integer.MAX_VALUE;
return;
}
nextTable = nextTab;
transferIndex = n; /** 当出现并发扩容时,这个全局变量,是用来给各个线程分配节点的。*/
}
1、首先,创建ForwardingNode,源码如下所示:
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2、数据结构如下所示:
3、开启无限循环
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; /** 下一个索引 */
int nextBound; /** 下一个边界 */
if (--i >= bound || finishing) { /** i执行-1操作,即:往前遍历一步 */
advance = false; /** 跳出while循环 */
}
else if ((nextIndex = transferIndex) <= 0) { /** transferIndex<=0,表示数据迁移的活儿都分配完毕了,不需要再划分范围执行迁移了 */
i = -1;
advance = false;
}
else if (U.compareAndSwapInt(this, TRANSFERINDEX, nextIndex,
nextBound = (nextIndex > stride ? nextIndex - stride : 0))) { /** nextBound表示下一个迁移的边界 */
bound = nextBound; /** 设置边界 */
i = nextIndex - 1; /** i表示需要遍历数组的下标,用于下面根据i来迁移对应的链表 */
advance = false; /** 因为advance=false,所以跳出while循环 */
}
}
... 下面代码后续我们在分析 ...
}
4、解释
if (i < 0 || i >= n || i + n >= nextn) {
... case1:扩容迁移结束逻辑 ...
}
else if ((f = tabAt(tab, i)) == null) {
... case2:如果下标i处没有节点,则不需要进行扩容迁移操作 ...
}
else if ((fh = f.hash) == MOVED) {
... case3:下标为i的这个位置已经被处理过了,设置advance = true,重新执行while(advance) ...
}
else {
... case4:执行扩容迁移操作 ...
}
1、源码如下
if (i < 0 || i >= n || i + n >= nextn) { /** 扩容的活儿都分配完毕了 */
int sc;
if (finishing) {
nextTable = null;
table = nextTab;
sizeCtl = (n << 1) - (n >>> 1); /** 设置新数组的阈值——sizeCtl=1.5*n=0.75*2n */
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
}
}
2、解释
1、源码如下
else if ((f = tabAt(tab, i)) == null) { /** 如果下标i处没有节点,则不需要进行扩容迁移操作 */
advance = casTabAt(tab, i, null, fwd);
}
2、解释
1、源码如下
else if ((fh = f.hash) == MOVED) { /** 下标为i的这个位置已经被处理过了,设置advance = true,重新执行while(advance) */
advance = true; // already processed
}
2、解释
1、源码如下
synchronized (f) {
if (tabAt(tab, i) == f) {
Node<K,V> ln; /** 低位的node元素 */
Node<K,V> hn; /** 高位的node元素 */
if (fh >= 0) {
int runBit = fh & n; /** runBit=0时,表明元素f在当前的位置不用移动;否则需要移动到新扩展的区域 */
Node<K,V> lastRun = f;
/** 遍历到链表的最后一个元素,跳出for循环 */
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) { /** 将存储在低位区的Node拼装成新的链表 */
ln = new Node<K,V>(ph, pk, pv, ln);
}
else { /** 将存储在高位区的Node拼装成新的链表 */
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) {
... 红黑树处理逻辑(不涉及讲解部分) ...
}
}
}
2、解释
public static int numberOfLeadingZeros(int i) {
if (i == 0)
return 32;
int n = 1;
/** * 00 | 00 | 0000 | 0000 0000 | 0000 0000 0000 0000 * [30] [28] [24] [16] */
/** 【32~17】(高16位)是否都为0,如果都是0,那么n=17,i左移16位,实现对高16位的清空操作 */
if (i >>> 16 == 0) { // eg1: 0100>>>16=0000
n += 16; // eg1: n=1+16=17
i <<= 16; // eg1: i=0100<<16= 0100 0000 0000 0000 0000
}
/** 【16~9】是否都为0 */
if (i >>> 24 == 0) {
n += 8; // eg1: n=17+8=25
i <<= 8; // eg1: i=0100 0000 0000 0000 0000 << 8 = 0100 0000 0000 0000 0000 0000 0000
}
/** 【8~5】是否都为0 */
if (i >>> 28 == 0) {
n += 4; // eg1: n=25+4=29
i <<= 4; // eg1: i=0100 0000 0000 0000 0000 0000 0000 << 4 = 0100 0000 0000 0000 0000 0000 0000 0000
}
/** 【4~3】是否都为0 */
if (i >>> 30 == 0) {
n += 2;
i <<= 2;
}
n -= i >>> 31; // eg1: n=29-(0100 0000 0000 0000 0000 0000 0000 0000 >>> 31)=29-0=29
return n;
}
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)) {
... 第一种情况,计算当前存入key-value的总数 ...
}
if (check >= 0) {
... 第二种情况,存储的总kv数量达到了阈值,执行扩容 ...
}
}
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))) { /** 使用CellValue来计数 */
fullAddCount(x, uncontended); /** 使用CounterCell来计数 */
return;
}
if (check <= 1) {
return;
}
/** 统计所有count值 */
s = sumCount();
}
private final void fullAddCount(long x, boolean wasUncontended) {
int h;
if ((h = ThreadLocalRandom.getProbe()) == 0) {
... 初始化操作随机数生成器 ...
}
/**【开启无限循环】*/
for (;;) {
/** * case1: counterCells不为空 且 数组里面有元素 */
if ((as = counterCells) != null && (n = as.length) > 0) {
f ((a = as[(n - 1) & h]) == null) {
...如果随机数h待插入的下标位置没有元素,则插入CounterCell ...
}
else if (!wasUncontended) { /** 如果wasUncontended=false(表示当前线程CAS竞争失败),则将wasUncontended重置为true */
wasUncontended = true;
}
else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x)) { /** 尝试将a的value值加x */
break;
}
else if (counterCells != as || n >= NCPU) {
collide = false; // At max size or stale
}
else if (!collide) {
collide = true;
}
else if (cellsBusy == 0 && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
... 多线程之间设置CounterCell的value值时发生碰撞,那么扩展CounterCell的长度,以减少碰撞次数 ...
}
... ...
}
/** * case2: cellsBusy为0且counterCells为空 */
else if (cellsBusy == 0 && counterCells == as && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
... 创建一个长度为2的CounterCell数组,将x赋值进数组,跳出循环 ...
}
/** * case3: 尝试修改baseCount的值 */
else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x)) {
... 如果修改成功,则跳出循环 ...
break;
}
}
CounterCell[] as;
CounterCell a;
int n;
long v;
if ((as = counterCells) != null && (n = as.length) > 0) {
if ((a = as[(n - 1) & h]) == null) { /** 如果随机数h待插入的下标位置没有元素 */
if (cellsBusy == 0) { // Try to attach new Cell
CounterCell r = new CounterCell(x); // Optimistic create
/** 通过CAS将cellsBusy设置为1,来表示正在操作CounterCell;操作完毕后,将cellsBusy设置为0,其他线程可以继续操作 */
if (cellsBusy == 0 && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
boolean created = false;
try { // Recheck under lock
CounterCell[] rs;
int m;
int j;
if ((rs = counterCells) != null && (m = rs.length) > 0 && rs[j = (m - 1) & h] == null) {
rs[j] = r; /** 在下标j处,插入新建的CounterCell r */
created = true;
}
} finally {
cellsBusy = 0;
}
if (created) {
break;
}
continue; // Slot is now non-empty
}
}
collide = false; /** collide:碰撞,cellsBusy不等于0时,表示有其他线程正在操作CounterCell,collide设置为false */
}
else if (!wasUncontended) { /** 如果wasUncontended=false(表示当前线程CAS竞争失败),则将wasUncontended重置为true */
wasUncontended = true; // CAS already known to fail,Continue after rehash
}
else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x)) { /** 尝试将a的value值加x */
break;
}
else if (counterCells != as || n >= NCPU) {
collide = false; // At max size or stale
}
else if (!collide) {
collide = true;
}
/** 多线程之间设置CounterCell的value值时发生碰撞,那么扩展CounterCell的长度,以减少碰撞次数 */
else if (cellsBusy == 0 && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
try {
if (counterCells == as) {// Expand table unless stale
CounterCell[] rs = new CounterCell[n << 1]; /** 根据原数组长度扩张2倍*/
for (int i = 0; i < n; ++i) {
rs[i] = as[i]; /** 进行数据迁移 */
}
counterCells = rs;
}
} finally {
cellsBusy = 0;
}
collide = false;
continue; // Retry with expanded table
}
/** 更改当前线程的探针哈希值 */
h = ThreadLocalRandom.advanceProbe(h);
}
/** *计算单元格表 *最初大小是2,如果线程较多,那么为了避免冲突,会进行2倍扩容。 *最终计算ConcurrentHashMap中所有元素的时候,其中一步,就是会统计 counterCells数组中所有元素的value值之和 */
private transient volatile CunterCell[] counterCells;
@sun.misc.Contended static dinal class CounterCell{
volatile Long value;
CounterCell(long x){
value=x;
}
}
class LongWithPadding {
long value;
long p0, p1, p2, p3, p4, p5, p6;
}
(2)、一个long占8个字节,再添加7个long属性就会变成64个字节,刚好是一个缓存行大小。
(3)、但是注意,Java7开始JVM做优化时可能会把不用的变量给去掉,所以这种方法并不推荐使用。
(4)、适用场景主要适用于频繁写的共享数据上。如果不是频繁写的数据,那么CPU缓存行被锁的几率就不多,所以没必要使用了,否则不仅占空间还会浪费CPU访问操作数据的时间。
if ((a = as[(n - 1) & h]) == null) { /** 如果随机数h待插入的下标位置没有元素 */
if (cellsBusy == 0) { // Try to attach new Cell
CounterCell r = new CounterCell(x); // Optimistic create
/** 通过CAS将cellsBusy设置为1,来表示正在操作CounterCell;操作完毕后,将cellsBusy设置为0,其他线程可以继续操作 */
if (cellsBusy == 0 && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
boolean created = false;
try { // Recheck under lock
CounterCell[] rs;
int m;
int j;
if ((rs = counterCells) != null && (m = rs.length) > 0 && rs[j = (m - 1) & h] == null) {
rs[j] = r; /** 在下标j处,插入新建的CounterCell r */
created = true;
}
} finally {
cellsBusy = 0;
}
if (created) {
break;
}
continue; // Slot is now non-empty
}
}
collide = false; /** collide:碰撞,cellsBusy不等于0时,表示有其他线程正在操作CounterCell,collide设置为false */
}
else if (!wasUncontended) {
wasUncontended = true; // CAS already known to fail,Continue after rehash
}
else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x)) {
break;
}
else if (counterCells != as || n >= NCPU) {
collide = false; // At max size or stale
}
else if (cellsBusy == 0 && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
try {
if (counterCells == as) {// Expand table unless stale
CounterCell[] rs = new CounterCell[n << 1]; /** 根据原数组长度扩张2倍*/
for (int i = 0; i < n; ++i) {
rs[i] = as[i]; /** 进行数据迁移 */
}
counterCells = rs;
}
} finally {
cellsBusy = 0;
}
collide = false;
continue; // Retry with expanded table
}
else if (cellsBusy == 0 && counterCells == as && U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
boolean init = false;
try { // Initialize table
if (counterCells == as) {
CounterCell[] rs = new CounterCell[2]; /** 创建长度为2的数组 */
rs[h & 1] = new CounterCell(x);
counterCells = rs;
init = true;
}
} finally {
cellsBusy = 0;
}
if (init) {
break;
}
}
else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x)) {
break; // Fall back on using base
}
final long sumCount() {
CounterCell[] as = counterCells; CounterCell a;
long sum = baseCount;
if (as != null) {
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
sum += a.value;
}
}
return sum;
}
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右移16位,则读取的就是原高16位的内容,即:table容量相关信息;不等于rs说明table容量发生了不一致的情况 */
sc == rs + 1 ||
sc == rs + MAX_RESIZERS ||
(nt = nextTable) == null ||
transferIndex <= 0) {
break;
}
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) { /** 加入1个共同扩容的线程,即:sc+1*/
transfer(tab, nt);
}
}
/** 执行扩容操作 */
else if (U.compareAndSwapInt(this, SIZECTL, sc, (rs << RESIZE_STAMP_SHIFT) + 2)) {
transfer(tab, null);
}
/** 统计所有count值 */
s = sumCount();
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/li1325169021/article/details/121310896
内容来源于网络,如有侵权,请联系作者删除!