5

I'm learning the java source code, when i reading the ConcurrentHashMap source code, i'm confused with the initTable() method, why check (tab = table) == null || tab.length == 0 twice, first in the while(), then in the if(). I can't imagine in what situation need the second check.

I think maybe it's because the JVM reorder the code, put sizeCtl = sc; in front of Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];. It's just my guess, I don't know is it right.

Can some one explain it, thanks a lot.

 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 || tab.length == 0) {
                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
                        @SuppressWarnings("unchecked")
                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
                        table = tab = nt;
                        sc = n - (n >>> 2);
                    }
                } finally {
                    sizeCtl = sc;
                }
                break;
            }
        }
        return tab;
    }
peter Char
  • 75
  • 5

1 Answers1

7

Multiple threads may compete to do this (see "initialization race" comment).

To paraphrase the code:

while(uninitialized) {
    acquire_lock(); //compareAndSwapInt...
    if(uninitialized) {
        do_init();
    }
}

The outer check is a cheap "unlocked" test. The inner one is in case someone else already succeeded between while and compareAndSwapInt.

  • Shouldn't it be possible for only one thread to successfully execute ```U.compareAndSwapInt(this, SIZECTL, sc, -1)```? – zysaaa Nov 18 '21 at 14:45
  • @zysaaa `sc = sizeCtl` is capturing the value before, if it's -1, it goes to yield. Most of the time it will get 0, because it was 0 in the while condition. But what if `sizeCtl` changed between while and `sc = sizeCtl`? `sc>0` will let you through compareAndSwapInt only to find the work has already been done... –  Nov 18 '21 at 15:16
  • now it's clear, thank you a lot. – peter Char Nov 19 '21 at 02:08
  • Then consider to accept this answer – zysaaa Nov 19 '21 at 02:26