ConcurrentHashMap#addCount() 为什么只要 CAS 失败了,就肯定不扩容了?

2020-07-12 19:56:35 +08:00
 amiwrong123
    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))) {
                 //执行到这里,要么 CAS 修改 baseCount 失败了,要么 CAS 修改某个 cell 的值失败了
                 //之后将直接 return
                fullAddCount(x, uncontended);
                return;
            }
            if (check <= 1)
                return;
            s = sumCount();
        }
        //扩容部分
        if (check >= 0) {
            Node<K,V>[] tab, nt; int n, sc;
            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();
            }
        }
    }

就是不大理解这个意思。

这样的做法,岂不是会导致,该扩容的时候不会扩容。因为毕竟只有 CAS 直接成功的线程,才可能扩容。

1524 次点击
所在节点    Java
2 条回复
jasonyee
2020-07-12 23:46:01 +08:00
CAS 失败说明被其他线程扩容了 就不需要本线程操作了
amiwrong123
2020-07-12 23:53:44 +08:00
@jasonyee
比如这种场景,线程 A CAS 成功,此时大小为阈值-1 ;然后线程 B CAS 失败,之后执行 fulladdcount 后大小刚好为阈值,但由于失败,不会去扩容;只有等到第 3 个线程到来,才可能去扩容了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/689370

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX