amiwrong123
V2EX  ›  Java

为什么 ConcurrentHashMap1.8 的扩容操作,最后一个完成任务的线程,还要再扫查一遍每个哈希桶呢?

  •  
  •   amiwrong123 · Jul 16, 2020 · 1610 views
    This topic created in 2138 days ago, the information mentioned may be changed or developed.
                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;//finishing 为 true,再次扫查
                        i = n; // 把遍历指针设置为 n,再从尾到头扫查一遍
                    }
                }
    

    transfer 函数里有这么一段代码,它让最后一个归还 sizeCtl 线程数的那个线程再做一遍扫查,但既然当前线程都已经是最后一个归还 sizeCtl 线程数那个线程,说明每个线程包括自己都已经完成了自己领取的 transfer 任务了,那不就没必要再从尾到头扫查一遍了吗?

    还是我遗漏了什么情况

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2821 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 06:00 · PVG 14:00 · LAX 23:00 · JFK 02:00
    ♥ Do have faith in what you're doing.