并发容器ConcurrentHashMap( 三 )
private final void tryPresize(int size) {//计算扩容的目标sizeint c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :tableSizeFor(size + (size >>> 1) + 1);int sc;while ((sc = sizeCtl) >= 0) {Node[] tab = table; int n;//tab没有初始化if (tab == null || (n = tab.length) == 0) {n = (sc > c) ? sc : c;//初始化之前 , CAS设置sizeCtl=-1if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {try {if (table == tab) {@SuppressWarnings("unchecked")Node[] nt = (Node[])new Node,?>[n];table = nt;//sc=0.75n,相当于扩容阈值sc = n - (n >>> 2);}} finally {//此时并没有通过CAS赋值 , 因为其他想要执行初始化的线程 , 发现sizeCtl=-1 , 就直接返回 , 从而确保任何情况 , 只会有一个线程执行初始化操作 。sizeCtl = sc;}}}//目标扩容size小于扩容阈值 , 或者容量超过最大限制时 , 不需要扩容else if (c <= sc || n >= MAXIMUM_CAPACITY)break;//扩容else if (tab == table) {int rs = resizeStamp(n);//sc<0表示 , 已经有其他线程正在扩容if (sc < 0) {Node[] nt;/**1 (sc >>> RESIZE_STAMP_SHIFT) != rs :扩容线程数 > MAX_RESIZERS-12 sc == rs + 1 和 sc == rs + MAX_RESIZERS :表示什么???3 (nt = nextTable) == null :表示nextTable正在初始化4 transferIndex <= 0 :表示所有hash桶均分配出去*///如果不需要帮其扩容 , 直接返回if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||transferIndex <= 0)break;//CAS设置sizeCtl=sizeCtl+1if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))//帮其扩容transfer(tab, nt);}//第一个执行扩容操作的线程 , 将sizeCtl设置为:(resizeStamp(n) << RESIZE_STAMP_SHIFT) + 2)else if (U.compareAndSwapInt(this, SIZECTL, sc,(rs << RESIZE_STAMP_SHIFT) + 2))transfer(tab, null);}}}
此段代码参考网址:transfer方法
private final void transfer(Node[] tab, Node[] nextTab) {int n = tab.length, stride;//计算需要迁移多少个hash桶(MIN_TRANSFER_STRIDE该值作为下限 , 以避免扩容线程过多)if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)stride = MIN_TRANSFER_STRIDE; // subdivide rangeif (nextTab == null) {// initiatingtry {//扩容一倍@SuppressWarnings("unchecked")Node[] nt = (Node[])new Node,?>[n << 1];nextTab = nt;} catch (Throwable ex) {// try to cope with OOMEsizeCtl = Integer.MAX_VALUE;return;}nextTable = nextTab;transferIndex = n;}int nextn = nextTab.length;ForwardingNode fwd = new ForwardingNode(nextTab);boolean advance = true;boolean finishing = false; // to ensure sweep before committing nextTab//1 逆序迁移已经获取到的hash桶集合 , 如果迁移完毕 , 则更新transferIndex , 获取下一批待迁移的hash桶//2 如果transferIndex=0 , 表示所以hash桶均被分配 , 将i置为-1 , 准备退出transfer方法for (int i = 0, bound = 0;;) {Node f; int fh;//更新待迁移的hash桶索引while (advance) {int nextIndex, nextBound;//更新迁移索引i 。if (--i >= bound || finishing)advance = false;else if ((nextIndex = transferIndex) <= 0) {//transferIndex<=0表示已经没有需要迁移的hash桶 , 将i置为-1 , 线程准备退出i = -1;advance = false;}//当迁移完bound这个桶后 , 尝试更新transferIndex ,, 获取下一批待迁移的hash桶else if (U.compareAndSwapInt(this, TRANSFERINDEX, nextIndex,nextBound = (nextIndex > stride ?nextIndex - stride : 0))) {bound = nextBound;i = nextIndex - 1;advance = false;}}//退出transferif (i < 0 || i >= n || i + n >= nextn) {int sc;if (finishing) {//最后一个迁移的线程 , recheck后 , 做收尾工作 , 然后退出nextTable = null;table = nextTab;sizeCtl = (n << 1) - (n >>> 1);return;}if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {/**第一个扩容的线程 , 执行transfer方法之前 , 会设置 sizeCtl = (resizeStamp(n) << RESIZE_STAMP_SHIFT) + 2)后续帮其扩容的线程 , 执行transfer方法之前 , 会设置 sizeCtl = sizeCtl+1每一个退出transfer的方法的线程 , 退出之前 , 会设置 sizeCtl = sizeCtl-1那么最后一个线程退出时:必然有sc == (resizeStamp(n) << RESIZE_STAMP_SHIFT) + 2) , 即 (sc - 2) == resizeStamp(n) << RESIZE_STAMP_SHIFT*///不相等 , 说明不到最后一个线程 , 直接退出transfer方法if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)return;finishing = advance = true;//最后退出的线程要重新check下是否全部迁移完毕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//迁移node节点else {synchronized (f) {if (tabAt(tab, i) == f) {Node ln, hn;//链表迁移if (fh >= 0) {int runBit = fhNode lastRun = f;for (Node p = f.next; p != null; p = p.next) {int b = p.hashif (b != runBit) {runBit = b;lastRun = p;}}if (runBit == 0) {ln = lastRun;hn = null;}else {hn = lastRun;ln = null;}//将node链表 , 分成2个新的node链表for (Node
- Store|苹果将在韩国开设第二家Apple Store直营店 并发布纪念壁
- Linux(服务器编程):百万并发服务器系统参数调优
- 3D打印高性能可拉伸微型超级电容器
- 为什么 Redis 单线程能支撑高并发?
- IT工程师都需要掌握的容器技术之Docker容器管理
- 除非有理由使用其他容器,优先使用STL vector
- 分布式锁结合SpringCache
- 苹果将在韩国开设第二家Apple Store直营店 并发布纪念壁纸
- Redis集群做法的难点,百万并发客户端「实战」
- 高并发的可见性搞不明白,就不用再研发了