对  1.8 的 ConcurrentHashMap 做一个小总结:

从数组开始

transient volatile Node<K,V>[] table;

扩容时用到

private transient volatile Node<K,V>[] nextTable;

哈希槽后面连接着链表,或者红黑树

节点哈希值的含义

static final int MOVED     = -1; // hash for forwarding nodes
static final int TREEBIN   = -2; // hash for roots of trees

操作数组使用的是 Unsafe

private static final sun.misc.Unsafe U;

对槽中连的第一个元素的操作使用的是 CAS,而 CAS 是有可能失败的,因此,put 是在 for 循环中完成,有重试的动作。

put 方法

final V putVal(K key, V value, boolean onlyIfAbsent) {
    // key 和 value 不能为空
    if (key == null || value == null) throw new NullPointerException();
    // 计算 hash 值
    int hash = spread(key.hashCode());
    int binCount = 0;
    // 请一定注意,put 操作是在 for 循环里面完成的
    for (Node<K,V>[] tab = table;;) {
        Node<K,V> f; int n, i, fh;
        if (tab == null || (n = tab.length) == 0)
            // table 还为空,初始化数组
            tab = initTable();
        // 对 hash 值取模
        // 用 Unsafe#getObjectVolatile 获取数组元素
        else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
            // 如果头节点为空,使用 Unsafe#compareAndSwapObject 设置头节点
            if (casTabAt(tab, i, null,
                         new Node<K,V>(hash, key, value, null)))
                break;                   // no lock when adding to empty bin
        }
        else if ((fh = f.hash) == MOVED)
            tab = helpTransfer(tab, f);
        else {
            // 如果头节点存在,则对头节点加锁
            V oldVal = null;
            synchronized (f) {
                if (tabAt(tab, i) == f) {
                    // 当 Node.hash 等于 -2,表明 TreeBin,大于 0 ,则是正常的链表
                    if (fh >= 0) {
                        binCount = 1;
                        // 遍历链表 Node
                        for (Node<K,V> e = f;; ++binCount) {
                            K ek;
                            // 遍历链表,如果发现相同的 key,onlyIfAbsent 默认 false,则替换 value
                            if (e.hash == hash &&
                                ((ek = e.key) == key ||
                                 (ek != null && key.equals(ek)))) {
                                oldVal = e.val;
                                if (!onlyIfAbsent)
                                    e.val = value;
                                break;
                            }
                            // 没有相同的 key,插入链表尾部
                            Node<K,V> pred = e;
                            if ((e = e.next) == null) {
                                pred.next = new Node<K,V>(hash, key,
                                                          value, null);
                                break;
                            }
                        }
                    }
                    else if (f instanceof TreeBin) {
                        Node<K,V> p;
                        binCount = 2;
                        // 节点插入红黑树中
                        if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
                                                       value)) != null) {
                            oldVal = p.val;
                            if (!onlyIfAbsent)
                                p.val = value;
                        }
                    }
                }
            }
            if (binCount != 0) {
                // 链表数量超过 8,且 table 数量大于 64, 则创建红黑树
                if (binCount >= TREEIFY_THRESHOLD)
                    treeifyBin(tab, i);
                if (oldVal != null)
                    return oldVal;
                // 跳出外层 for 循环
                break;
            }
        }
    }
    addCount(1L, binCount);
    return null;
}

HashMap 的数组长度是 2 的幂,决定一个元素插在哪个槽,本质是对 len 取模,对应到代码是 &(len - 1)

例如,哈希表的数组长度是 8,两个键的 hash 值分别是 2 和 10
2 mod 8 = 2
10 mod 8 = 2
在扩容时,长度从 8 扩到 16
2 mod 16 = 2
10 mod 16 = 10
2 对应的槽位不变,10 对应的槽位变了

当 map 中节点数量超过容量时,或者链表长度大于8,但数组长度小于64时,会触发(纵向)扩容。扩容是多线程并发扩容,可以认为是每个线程处理 16 个槽。

原数组中,迁移的槽位号,从 n 到 0

private transient volatile int transferIndex;
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
    int n = tab.length, stride;
    // stride 是每个线程需要处理的槽个数,暂认为是 16
    if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
        stride = MIN_TRANSFER_STRIDE; // subdivide range
    // 如果 nextTab 为空,则创建数组
    if (nextTab == null) {            // initiating
        try {
            @SuppressWarnings("unchecked")
            Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
            nextTab = nt;
        } catch (Throwable ex) {      // try to cope with OOME
            sizeCtl = Integer.MAX_VALUE;
            return;
        }
        nextTable = nextTab;
        // 设置 transferIndex,线程从 transferIndex 开始,递减处理 16 个槽
        transferIndex = n;
    }
    int nextn = nextTab.length;
    // 迁移完的标志节点
    ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
    boolean advance = true;
    boolean finishing = false; // to ensure sweep before committing nextTab
    for (int i = 0, bound = 0;;) {
        Node<K,V> f; int fh;
        // 这个 while 循环确定线程要处理的边界
        // 这个 i 就是线程开始处理的原 table 的槽位置
        while (advance) {
            int nextIndex, nextBound;
            if (--i >= bound || finishing)
                advance = false;
            else if ((nextIndex = transferIndex) <= 0) {
                i = -1;
                advance = false;
            }
            else if (U.compareAndSwapInt
                     (this, TRANSFERINDEX, nextIndex,
                      nextBound = (nextIndex > stride ?
                                   nextIndex - stride : 0))) {
                bound = nextBound;
                i = nextIndex - 1;
                advance = false;
            }
        }
        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;
                i = n; // recheck before commit
            }
        }
        // 原 table 槽 i 处的键为空
        else if ((f = tabAt(tab, i)) == null)
            // 直接置为已迁移完标志
            advance = casTabAt(tab, i, null, fwd);
        // 已迁移完
        else if ((fh = f.hash) == MOVED)
            advance = true; // already processed
        else {
            synchronized (f) {
                if (tabAt(tab, i) == f) {
                    // 低位节点,高位节点
                    // 低位节点插在新 table 的槽位不变,高位节点插在新 table 槽位 i+n 处
                    Node<K,V> ln, hn;
                    if (fh >= 0) {
                        // 键的哈希值与上原数组长度
                        int runBit = fh & n;
                        // lastRun 表示最后的连续的高位为1或0的节点链表
                        Node<K,V> lastRun = f;
                        for (Node<K,V> p = f.next; p != null; p = p.next) {
                            int b = p.hash & n;
                            if (b != runBit) {
                                runBit = b;
                                lastRun = p;
                            }
                        }
                        if (runBit == 0) {
                            ln = lastRun;
                            hn = null;
                        }
                        else {
                            hn = lastRun;
                            ln = null;
                        }
                        for (Node<K,V> p = f; p != lastRun; p = p.next) {
                            int ph = p.hash; K pk = p.key; V pv = p.val;
                            // 哈希值与上原数组长度为 0,插入新数组槽 i
                            if ((ph & n) == 0)
                                ln = new Node<K,V>(ph, pk, pv, ln);
                            // 哈希值与上原数组长度不为 0,插入新数组槽 i+n
                            else
                                hn = new Node<K,V>(ph, pk, pv, hn);
                        }
                        setTabAt(nextTab, i, ln);
                        setTabAt(nextTab, i + n, hn);
                        setTabAt(tab, i, fwd);
                        advance = true;
                    }
                    else if (f instanceof TreeBin) {
                        TreeBin<K,V> t = (TreeBin<K,V>)f;
                        TreeNode<K,V> lo = null, loTail = null;
                        TreeNode<K,V> hi = null, hiTail = null;
                        int lc = 0, hc = 0;
                        for (Node<K,V> e = t.first; e != null; e = e.next) {
                            int h = e.hash;
                            TreeNode<K,V> p = new TreeNode<K,V>
                                (h, e.key, e.val, null, null);
                            if ((h & n) == 0) {
                                if ((p.prev = loTail) == null)
                                    lo = p;
                                else
                                    loTail.next = p;
                                loTail = p;
                                ++lc;
                            }
                            else {
                                if ((p.prev = hiTail) == null)
                                    hi = p;
                                else
                                    hiTail.next = p;
                                hiTail = p;
                                ++hc;
                            }
                        }
                        ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
                            (hc != 0) ? new TreeBin<K,V>(lo) : t;
                        hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
                            (lc != 0) ? new TreeBin<K,V>(hi) : t;
                        setTabAt(nextTab, i, ln);
                        setTabAt(nextTab, i + n, hn);
                        setTabAt(tab, i, fwd);
                        advance = true;
                    }
                }
            }
        }
    }
}

-1 初始化
-(1+扩容线程数)
扩容阈值

当 map 中元素超过 sizeCtl 时,开始扩容

private transient volatile int sizeCtl;
public ConcurrentHashMap(int initialCapacity) {
    if (initialCapacity < 0)
        throw new IllegalArgumentException();
    int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
               MAXIMUM_CAPACITY :
               tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
    // 初始值 cap,规格化 2 的幂
    this.sizeCtl = cap;
}

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
        // 设置 -1,开始初始化
        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;
                    // n - 0.25n = 0.75n
                    sc = n - (n >>> 2);
                }
            } finally {
                sizeCtl = sc;
            }
            break;
        }
    }
    return tab;
}

统计 map 中的数量

private transient volatile long baseCount;

使用 CAS 写 baseCount 变量,更新成功就成功,失败了需要再次更新。

链表转化为红黑树,当链表长度超过 8,且数组长度大于 64 时,树化(横向扩容)

private final void treeifyBin(Node<K,V>[] tab, int index) {
    Node<K,V> b; int n, sc;
    if (tab != null) {
        // 数组长度小于 64,数组扩容成 2n
        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));
                }
            }
        }
    }
}

get 方法

public V get(Object key) {
    Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
    int h = spread(key.hashCode());
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (e = tabAt(tab, (n - 1) & h)) != null) {
        if ((eh = e.hash) == h) {
            if ((ek = e.key) == key || (ek != null && key.equals(ek)))
                return e.val;
        }
        // 节点的哈希值小于 0,可能是红黑树或者 ForwardingNode(扩容完成迁移)
        else if (eh < 0)
            return (p = e.find(h, key)) != null ? p.val : null;
        // 遍历链表查询
        while ((e = e.next) != null) {
            if (e.hash == h &&
                ((ek = e.key) == key || (ek != null && key.equals(ek))))
                return e.val;
        }
    }
    return null;
}

get 操作的槽位是 ForwardingNode 节点时,查询操作路由到新的数组中

// java.util.concurrent.ConcurrentHashMap.ForwardingNode#find
Node<K,V> find(int h, Object k) {
    // loop to avoid arbitrarily deep recursion on forwarding nodes
    outer: for (Node<K,V>[] tab = nextTable;;) {
        Node<K,V> e; int n;
        if (k == null || tab == null || (n = tab.length) == 0 ||
            (e = tabAt(tab, (n - 1) & h)) == null)
            return null;
        for (;;) {
            int eh; K ek;
            if ((eh = e.hash) == h &&
                ((ek = e.key) == k || (ek != null && k.equals(ek))))
                return e;
            if (eh < 0) {
                if (e instanceof ForwardingNode) {
                    tab = ((ForwardingNode<K,V>)e).nextTable;
                    continue outer;
                }
                else
                    return e.find(h, k);
            }
            if ((e = e.next) == null)
                return null;
        }
    }
}

 

场景1:在数组扩容时,插入键值对
当前线程 put 键值对时,如果插入的槽位正在迁移,因为这个槽被迁移线程加锁了,所以当前线程阻塞等待锁,槽位被其他线程迁移完后,置节点为 ForwardingNode,当前线程获得锁,因为槽位不是普通的链表和树,则开始 for 循环下一轮遍历,发现槽位是 ForwardingNode,则帮助迁移,当前线程完成自己的迁移任务后,返回新的 table,把键值对插入新的 table 中。

 

场景2:事实上,一个线程在扩容时只处理 16 个槽,所以有一段时间,数据会分布在新旧两个数组中。

posted on 2020-02-09 13:48  偶尔发呆  阅读(181)  评论(0)    收藏  举报