Java - ConcurrentHashMap 源码阅读笔记

in Tech Java

简介

  ConcurrentHashMap 顾名思义在HashMap的基础上加锁来保障并发操作时其内部数据的原子性。但是与HashTable直接用synchronized锁定全表不同。ConcurrentHashMap 使用了Unsafe类的CAS + synchronized + LockSupport,仅锁定table中的单个元素,实现了高效并发的需求。其中,最具学习的算法是将扩容任务拆分成多个子任务,然后让多个线程同时参与扩容任务。

数据结构

  与HashMap基本一致。唯一不一样的点是,红黑树结构时,存在数组里的节点为TreeBin,然后由TreeBin来管理红黑树。
ConcurrentHashMap数据结构.jpg

源码

构造方法

    // initialCapacity 初始容量;loadFactor 加载因子;concurrencyLevel 预估并发数
    public ConcurrentHashMap() {
    }
    public ConcurrentHashMap(int initialCapacity) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException();
        // 计算容量。只能是2次幂,所以需要做转化。
        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
                   MAXIMUM_CAPACITY :
                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
        this.sizeCtl = cap;
    }
    // 指定的加载因子,仅仅是初始化时使用。后续扩容均按0.75来计算阀值。这点跟HashMap是不一样的。
    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
        this(initialCapacity, loadFactor, 1);
    }
    public ConcurrentHashMap(int initialCapacity,
                             float loadFactor, int concurrencyLevel) {
        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
            throw new IllegalArgumentException();
        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
            initialCapacity = concurrencyLevel;   // as estimated threads
        // 计算容量
        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
            MAXIMUM_CAPACITY : tableSizeFor((int)size);
        this.sizeCtl = cap;
    }

新增or修改节点

put(K key, V value)方法大致流程如下:

  1. 不允许key或value为null。这点与HashMap不同;
  2. 若table为空,则初始化table;
  3. 若table[i]为空,则通过CAS操作插入新节点,转向第六步;
  4. 若table[i]的哈希值为MOVED,则说明正在进行扩容任务,让当前线程参与扩容;
  5. 给table[i]加同步锁synchronized。table[i]的hash大于等于0,则为链表类型,否则为红黑树类型,进行相应的更新操作;
  6. 若binCount大于8且table长度小于64,则执行tryPresize()进行扩容,否则把原单向链表转化为双向链表,再由双向链表转化为红黑树;
  7. 调用addCount方法更新SIZE、扩容检查等;

put(K key, V value)方法

    public V put(K key, V value) {
        return putVal(key, value, false);
    }

    final V putVal(K key, V value, boolean onlyIfAbsent) {
        // 不允许key或value为空
        if (key == null || value == null) throw new NullPointerException();
        int hash = spread(key.hashCode());
        int binCount = 0;
        // 自旋
        for (Node<K,V>[] tab = table;;) {
            Node<K,V> f; int n, i, fh;
            if (tab == null || (n = tab.length) == 0)
                // 初始化table
                tab = initTable();
            // 同过Unsafe类来获取最新值
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
                //同过Unsafe类的CAS锁插入数据
                if (casTabAt(tab, i, null,new Node<K,V>(hash, key, value, null)))
                    break;                   // no lock when adding to empty bin
            }
            // 正在扩容。f节点为ForwardingNode
            else if ((fh = f.hash) == MOVED)
                // 让当前线程参与扩容
                tab = helpTransfer(tab, f);
            else {
                V oldVal = null;
                synchronized (f) {
                    // 因为多线程,当前值可能在加锁前被改变。
                    if (tabAt(tab, i) == f) {
                        // hash值大于等于0,则为链表
                        if (fh >= 0) {
                            binCount = 1;
                            for (Node<K,V> e = f;; ++binCount) {
                                K ek;
                                // 已存在则更新
                                if (e.hash == hash &&
                                    ((ek = e.key) == key ||
                                     (ek != null && key.equals(ek)))) {
                                    oldVal = e.val;
                                    if (!onlyIfAbsent)
                                        e.val = value;
                                    break;
                                }
                                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,则开始树化操作
                    if (binCount >= TREEIFY_THRESHOLD)
                        treeifyBin(tab, i);
                    if (oldVal != null)
                        return oldVal;
                    break;
                }
            }
        }
        // 计数&扩容检查等
        addCount(1L, binCount);
        return null;
    }

initTable()方法

 //初始化table
 private final Node<K,V>[] initTable() {
        Node<K,V>[] tab; int sc;
        while ((tab = table) == null || tab.length == 0) {
            // sc小于0,则说明,初始化操作已经被其他线程处理了。
            if ((sc = sizeCtl) < 0)
                // 让出CPU时间。使当前线程由执行状态,变成为就绪状态,重新抢占CPU时间。
                Thread.yield(); // lost initialization race; just spin
            // CAS操作将sizeCtl的值改为-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;
                        // 相当于sc = n*0.75;
                        sc = n - (n >>> 2);
                    }
                } finally {
                    // 最后给阀值赋值。
                    sizeCtl = sc;
                }
                break;
            }
        }
        return tab;
    }

treeifyBin(Node<K,V>[] tab, int index)方法

    private final void treeifyBin(Node<K,V>[] tab, int index) {
        Node<K,V> b; int n, sc;
        if (tab != null) {
            // table的长度小于64的话,则进行扩容操作
            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;
                        }
                        // new TreeBin<K,V>(hd) 构造方法中会将hash修改为MOVED,并进行树化操作。
                        setTabAt(tab, index, new TreeBin<K,V>(hd));
                    }
                }
            }
        }
    }

addCount(long x, int check)方法

    private final void addCount(long x, int check) {
        //多线程竞争修改baseCount失败的计数对象管理
        CounterCell[] as; long b, s;
        if ((as = counterCells) != null ||
            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
            CounterCell a; long v; int m;
            boolean uncontended = true;
            if (as == null || (m = as.length - 1) < 0 ||
                (a = as[ThreadLocalRandom.getProbe() & m]) == null ||
                !(uncontended =
                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
                fullAddCount(x, uncontended);
                return;
            }
            if (check <= 1)
                return;
            // 用BASECOUNT加上所有线程增加的长度,即CounterCell数组中的value值相加+BASECOUNT
            s = sumCount();
        }

        // 如果的binCount大于等于0,需要去检查是否需要扩容及进行计数
        if (check >= 0) {
            Node<K,V>[] tab, nt; int n, sc;
            // 条目数量大于阈值sizeCtl
            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
                   (n = tab.length) < MAXIMUM_CAPACITY) {
                // 生成一个扩容戳。如n=16,则rs为1000000000011011
                int rs = resizeStamp(n);

                //sizeCtl<0,表示有线程在进行扩容
                if (sc < 0) {
                    // sc >>> RESIZE_STAMP_SHIFT!=rs,判断高位的扩容标记不相同,则不能参与扩容
                    // sc == rs + 1,表示扩容已经结束
                    // sc == rs + MAX_RESIZERS ,表示当前帮助扩容的线程数已经达到最大值
                    // (nt = nextTable) == null,表示扩容已经结束
                    // transferIndex <= 0,表示所有的 transfer 任务都被领取完了
                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
                        transferIndex <= 0)
                        break;
                    // 并增加线程标记
                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
                        // 协助扩容
                        transfer(tab, nt);
                }
                // 当前线程开始扩容,将rs左移16位变为负数,并增加线程标记
                else if (U.compareAndSwapInt(this, SIZECTL, sc,
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
                    // 开始扩容
                    transfer(tab, null);
                s = sumCount();
            }
        }
    }

查找节点

get(Object key)方法

    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;
            }
            // hash值小于0为红黑树
            else if (eh < 0)
                // 这里的find(int h, Object k)方法是调用TreeBin类的重写方法。不要被表象迷惑啦!
                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;
    }

删除节点

remove(Object key)方法

    public V remove(Object key) {
        return replaceNode(key, null, null);
    }
    final V replaceNode(Object key, V value, Object cv) {
        int hash = spread(key.hashCode());
        for (Node<K,V>[] tab = table;;) {
            Node<K,V> f; int n, i, fh;
            if (tab == null || (n = tab.length) == 0 ||
                (f = tabAt(tab, i = (n - 1) & hash)) == null)
                break;
            else if ((fh = f.hash) == MOVED)
                // 协助运输扩容任务
                tab = helpTransfer(tab, f);
            else {
                V oldVal = null;
                // 是否拿到锁
                boolean validated = false;
                synchronized (f) {
                    if (tabAt(tab, i) == f) {
                        // 链表
                        if (fh >= 0) {
                            validated = true;
                            for (Node<K,V> e = f, pred = null;;) {
                                K ek;
                                if (e.hash == hash &&
                                    ((ek = e.key) == key ||
                                     (ek != null && key.equals(ek)))) {
                                    V ev = e.val;
                                    // 删除节点的前后节点互联
                                    if (cv == null || cv == ev ||
                                        (ev != null && cv.equals(ev))) {
                                        oldVal = ev;
                                        if (value != null)
                                            e.val = value;
                                        else if (pred != null)
                                            pred.next = e.next;
                                        else
                                            setTabAt(tab, i, e.next);
                                    }
                                    break;
                                }
                                pred = e;
                                if ((e = e.next) == null)
                                    break;
                            }
                        }
                        else if (f instanceof TreeBin) {
                            validated = true;
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
                            TreeNode<K,V> r, p;
                            if ((r = t.root) != null &&
                                (p = r.findTreeNode(hash, key, null)) != null) {
                                V pv = p.val;
                                if (cv == null || cv == pv ||
                                    (pv != null && cv.equals(pv))) {
                                    oldVal = pv;
                                    if (value != null)
                                        p.val = value;
                                    else if (t.removeTreeNode(p))
                                        setTabAt(tab, i, untreeify(t.first));
                                }
                            }
                        }
                    }
                }
                if (validated) {
                    if (oldVal != null) {
                        if (value == null)
                            addCount(-1L, -1);
                        return oldVal;
                    }
                    break;
                }
            }
        }
        return null;
    }

扩容

transfer(Node<K,V>[] tab, Node<K,V>[] nextTab)方法

private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
        // stride 步长,也就是预分配给每个线程处理的个数。
        int n = tab.length, stride;
        // NCPU 可用线程数。如笔记本双核四线程,则NCPU=4。
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
            stride = MIN_TRANSFER_STRIDE; // subdivide range

         // 初始化新table
        if (nextTab == null) {            // initiating
            try {
                @SuppressWarnings("unchecked")
                // 2倍扩容
                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 = 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 (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 ,这样addCount()检测到之后就无需再进来啦
                    nextTable = null;
                    // 更新全局变量
                    table = nextTab;
                    // 更新阀值
                    sizeCtl = (n << 1) - (n >>> 1);
                    return;
                }
                // CAS操作sc-1,表示当前线程完成扩容任务
                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
                }
            }
            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) {
                        Node<K,V> ln, hn;
                        // 链表
                        if (fh >= 0) {
                            int runBit = fh & n;
                            // 链尾节点
                            Node<K,V> lastRun = f;
                            //找出最后一段完整的 fh&n 不变的链表,这样最后这一段链表就不用重新创建新结点了。
                            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;
                            }

                            // 拆分成两个链表,且lastRun之前的结点因为fh&n不确定,所以全部需要重新迁移。
                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
                                int ph = p.hash; K pk = p.key; V pv = p.val;
                                if ((ph & n) == 0)
                                    ln = new Node<K,V>(ph, pk, pv, ln);
                                else
                                    hn = new Node<K,V>(ph, pk, pv, hn);
                            }
                            // 同HashMap一样。一个链表在原位置
                            setTabAt(nextTab, i, ln);
                            // 一个链表在 原索引+n 的位置
                            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;
                                }
                            }
                            // 需要树化,则进行树化操作。否则,由TreeNode转为Node
                            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;
                            //  同HashMap一样。一个链表在原位置
                            setTabAt(nextTab, i, ln);
                            // 一个链表在 原索引+n 的位置
                            setTabAt(nextTab, i + n, hn);
                            // 插入标记节点占位
                            setTabAt(tab, i, fwd);
                            advance = true;
                        }
                    }
                }
            }
        }
    }

helpTransfer(Node<K,V>[] tab, Node<K,V> f)方法

    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
        Node<K,V>[] nextTab; int sc;
        if (tab != null && (f instanceof ForwardingNode) &&
            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
            // 扩容戳
            int rs = resizeStamp(tab.length);
            // 扩容尚未结束
            while (nextTab == nextTable && table == tab &&
                   (sc = sizeCtl) < 0) {
                // 跟addCount()方法一样
                if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
                    sc == rs + MAX_RESIZERS || transferIndex <= 0)
                    break;
                if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
                    transfer(tab, nextTab);
                    break;
                }
            }
            return nextTab;
        }
        return table;
    }