ConcurrentHashMap tree list treeifyBin

private final void treeifyBin(Node<K,V>[] tab, int index) {
        Node<K,V> b; int n, sc;
        if (tab != null) {
            if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
                tryPresize(n << 1);
            else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
                synchronized (b) {
                    if (tabAt(tab, index) == b) {
                        TreeNode<K,V> hd = null, tl = null;
                        for (Node<K,V> e = b; e != null; e = e.next) {
                            TreeNode<K,V> p =
                                new TreeNode<K,V>(e.hash, e.key, e.val,
                                                  null, null);
                            if ((p.prev = tl) == null)
                                hd = p;
                            else
                                tl.next = p;
                            tl = p;
                        }
                        setTabAt(tab, index, new TreeBin<K,V>(hd));
                    }
                }
            }
        }
    }

In fact, like sewing, hd is the thread, tl is the end of the line, first of all, thread and wire tail are plugged in the first node p, p1 is the second time that prev points p, next to the new end of line p, p is the next point to the second node p1. Cycles, first organized in the new list, then put this list tree.

FIG example as follows

 

Guess you like

Origin www.cnblogs.com/javaddd/p/12293609.html