小编pet*_*har的帖子

ConcurrentHashMap.initTable(),为什么两次检查table为null?

我正在学习java源代码,当我阅读ConcurrentHashMap源代码时,我对initTable()方法感到困惑,为什么要检查(tab = table) == null || tab.length == 0两次while(),首先在if(). 我无法想象什么情况下需要第二次检查。

我想也许是因为 JVM 对代码重新排序,sizeCtl = sc;放在Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];. 这只是我的猜测,我不知道是否正确。

谁能解释一下,万分感谢。

 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(); // lost initialization race; just spin
            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
                try {
                    if ((tab = table) == null || …
Run Code Online (Sandbox Code Playgroud)

java concurrenthashmap

5
推荐指数
1
解决办法
132
查看次数

标签 统计

concurrenthashmap ×1

java ×1