并发编程

JDK 1.8中,CHM采用Node数组 + 链表/红黑树(避免hash冲突)

  • 细化锁 - 1.8中,只对数组元素进行加锁,进一步避免冲突(1.7中为分段锁的设计)
  • 纳入红黑树的实现,当链表长度>=8(且Map.size>=64)时,会将链表转换为红黑树(查询效率 log(n))

put

  • tip 1 : CHM中数组元素只在使用时才进行初始化
  • tip 2 : 对于为空的单元格,采用cas直接设值(无需锁定)
  • tip 3 : 发生线程冲突时,sync锁住单元格中数据结构的头节点,(根据Node节点类型进行链表/树操作)
  • tip 4 : CHM中的扩容,采用了分组的思想,通过标志位的控制,允许多个线程并发参与transfer
  • tip 5 : CHM中的size记录,也采用了分组的思想,baseCount + sum(CounterCell[]),避免对单一属性修改的并发问题
    public V put(K key, V value) {
        return putVal(key, value, false);
    }

    /** Implementation for put and putIfAbsent */
    final V putVal(K key, V value, boolean onlyIfAbsent) {
        if (key == null || value == null) throw new NullPointerException();
        // MAP 1 : 计算hash值,高低位相与,增强散列度(低位 ^ 高位>>>16) & 0x7fffffff 保证结果一定为正数
        int hash = spread(key.hashCode());
        int binCount = 0;   // 记录链表长度,用于链表与树的转换
        for (Node<K,V>[] tab = table;;) {   // 自旋
            Node<K,V> f; int n, i, fh;
            // MAP 2 - tip :初始化tab数组
            if (tab == null || (n = tab.length) == 0)
                tab = initTable();
            // tip :如果对应下标内容为空,cas插入值 -- (n-1)&hash,index取决于hash的后log(n)位
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
                if (casTabAt(tab, i, null,
                             new Node<K,V>(hash, key, value, null)))
                    break;                   // no lock when adding to empty bin
            }
            // MAP 3 :线程辅助扩容 fh,节点标志位,用于区分节点当前类型(-1 - 扩容中,>0 链表,-2 树...)
            else if ((fh = f.hash) == MOVED)
                tab = helpTransfer(tab, f);
            else {
                // MAP 4 :插入,但存在hash冲突,需加锁保证线程安全(只锁Node节点),并记录链表长度binCount,大于8时触发树转
                V oldVal = null;
                synchronized (f) {
                    if (tabAt(tab, i) == f) {
                        // tip : 链表结构插入 - 通过f.hash的值表示当前节点的状态,-1 扩容 -2 树 正数 普通链表节点
                        if (fh >= 0) {
                            binCount = 1;
                            // 查找是否存在相等的值,是则替换
                            for (Node<K,V> e = f;; ++binCount) {
                                K ek;
                                // key相等,覆盖
                                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;
                                // key均不相等,新建节点,并置于链表末端
                                if ((e = e.next) == null) {
                                    pred.next = new Node<K,V>(hash, key,
                                                              value, null);
                                    break;
                                }
                            }
                        }
                        // tip :树结构插入
                        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;
                            }
                        }
                    }
                }
                // MAP 5 :链表转红黑树
                if (binCount != 0) {
                    if (binCount >= TREEIFY_THRESHOLD)
                        treeifyBin(tab, i);
                    if (oldVal != null)
                        return oldVal;
                    break;
                }
            }
        }
        // MAP : 计数值加一(分段式增加) - 可能触发扩容操作
        addCount(1L, binCount);
        return null;
    }

initTable

通过CAS + 占位符完成并发控制

    // 初始化Table,标志位 sizeCtl:-1 正在初始化 -N 存在多个线程参与扩容 正数 下一次扩容阈值
    private final Node<K,V>[] initTable() {
        Node<K,V>[] tab; int sc;
        // 当table为空,未被初始化时进入初始化逻辑
        while ((tab = table) == null || tab.length == 0) {
            if ((sc = sizeCtl) < 0)
                Thread.yield(); // 其他线程获取了sizeCtl标志,则当前线程释放cpu时间片

            // MAP :采用sizeCtl标志位进行初始化并发控制,采用CAS实现线程安全 -- 初始化完成后,设置扩容阈值sizeCtl(75%)
            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {  // 线程CAS抢占初始化资格
                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;
    }

addCount

如果对baseCount的修改发生并发争抢,则随机获取CounterCells数组中的某个元素,并修改元素中的值(分组思想)

    // baseCount - 基础计数值
    // CounterCell - 分段计数组
    // size = baseCount + CounterCell.sum();
    private final void addCount(long x, int check) {
        CounterCell[] as; long b, s;

        // tip 1: 如果直接修改baseCount成功,则不进入if代码块
        // 否则,随机增加CounterCell中某一个Cell的值
        if ((as = counterCells) != null ||
            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
            // tip 2:修改baseCount失败,进入修改CounterCell逻辑
            CounterCell a; long v; int m;
            boolean uncontended = true;
            // tip 2:获取随机数,CAS修改对应下标位的值,如果成功,则不进入代码块
            // 否则,fullAddCount
            if (as == null || (m = as.length - 1) < 0 ||
                (a = as[ThreadLocalRandom.getProbe() & m]) == null ||
                !(uncontended =
                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
                // tip 3:如果①数组未初始化,②对应位置的对象未创建,③对应位置存在并发冲突,则
                fullAddCount(x, uncontended);
                return;
            }
            if (check <= 1)
                return;
            s = sumCount();
        }

        if (check >= 0) {
            Node<K,V>[] tab, nt; int n, sc;
            // size 大于 扩容阈值
            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
                   (n = tab.length) < MAXIMUM_CAPACITY) {
                int rs = resizeStamp(n);
                // sc < 0,表示当前已有线程处于扩容,尝试辅助扩容
                if (sc < 0) {
                    // 无需参与扩容
                    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);
                }
                // 第一次扩容(sc,高位为扩容标记,低位为参与扩容的线程数量)
                else if (U.compareAndSwapInt(this, SIZECTL, sc,
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
                    transfer(tab, null);
                s = sumCount();
            }
        }
    }

    private final void fullAddCount(long x, boolean wasUncontended) {
        int h;
        if ((h = ThreadLocalRandom.getProbe()) == 0) {
            ThreadLocalRandom.localInit();      // force initialization
            h = ThreadLocalRandom.getProbe();
            wasUncontended = true;
        }
        boolean collide = false;                // True if last slot nonempty
        for (;;) {
            CounterCell[] as; CounterCell a; int n; long v;
            // 已初始化
            if ((as = counterCells) != null && (n = as.length) > 0) {
                // 数组中对应下标位置未赋值
                if ((a = as[(n - 1) & h]) == null) {
                    // 构建CounterCell对象,并赋值
                    if (cellsBusy == 0) {            // Try to attach new Cell
                        CounterCell r = new CounterCell(x); // Optimistic create
                        if (cellsBusy == 0 &&
                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {   // 抢占CounterCell对象构建资格
                            boolean created = false;
                            try {               // Recheck under lock
                                CounterCell[] rs; int m, j;
                                if ((rs = counterCells) != null &&
                                    (m = rs.length) > 0 &&
                                    rs[j = (m - 1) & h] == null) {
                                    rs[j] = r;
                                    created = true;
                                }
                            } finally {
                                cellsBusy = 0;
                            }
                            if (created)
                                break;
                            continue;           // Slot is now non-empty
                        }
                    }
                    collide = false;
                }
                else if (!wasUncontended)       // CAS already known to fail
                    wasUncontended = true;      // Continue after rehash
                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))    // cas尝试设置值
                    break;
                else if (counterCells != as || n >= NCPU)   // 数组长度大于cpu核心数时,不再扩容
                    collide = false;
                else if (!collide)
                    collide = true;
                // 更新counterCells中元素失败,最终触发counterCells扩容操作
                else if (cellsBusy == 0 &&
                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
                    try {
                        if (counterCells == as) {// Expand table unless stale
                            CounterCell[] rs = new CounterCell[n << 1];
                            for (int i = 0; i < n; ++i)
                                rs[i] = as[i];
                            counterCells = rs;
                        }
                    } finally {
                        cellsBusy = 0;
                    }
                    collide = false;
                    continue;                   // Retry with expanded table
                }
                // 当发生线程争用后,改变ThreadLocalRandom的probe
                h = ThreadLocalRandom.advanceProbe(h);
            }

            // 未初始化 - cellsBusy 扩容占位符,初始化后数组长度为2,同时完成size add操作
            else if (cellsBusy == 0 && counterCells == as &&
                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
                boolean init = false;
                try {                           // Initialize table
                    if (counterCells == as) {
                        CounterCell[] rs = new CounterCell[2];
                        rs[h & 1] = new CounterCell(x);
                        counterCells = rs;
                        init = true;
                    }
                } finally {
                    cellsBusy = 0;
                }
                if (init)
                    break;
            }
            // 再次尝试 BASECOUNT (优化 - 多次尝试)
            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
                break;                          // Fall back on using base
        }
    }

transfer

扩容,分组扩容,根据cpu和size大小,计算出每一参与线程负责的node数量与范围
高低位指针,CHM中node数组的大小一定为2的幂,扩容时,node节点的数据只会分流到i或n+i,只需判断对应位置的bit值即可实现分流

    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
        int n = tab.length, stride;
        // tip : 通过cpu核心数计算出单个线程处理的node数
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
            stride = MIN_TRANSFER_STRIDE; // subdivide range
        // nextTab未初始化,则新建一个nextTab,(容量乘2)
        if (nextTab == null) {
            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 = n;          // 需要转移的节点个数
        }
        int nextn = nextTab.length;

        // 创建一个fwd节点(Hash = -1 MOVED),用于将当前节点的操作引向nextTab
        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
        boolean advance = true;     // 用于控制循环
        boolean finishing = false;  // 用于判断扩容是否完成
        for (int i = 0, bound = 0;;) {
            Node<K,V> f; int fh;

            // 自旋尝试为当前线程分配transfer任务(获取下标值,边界值和修改TRANSFERINDEX的值) - 处理区间为(nextBound,nextIndex)
            while (advance) {
                int nextIndex, nextBound;
                if (--i >= bound || finishing)      // 此处发生了i值的修改,将会导致跳出while循环
                    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)) {   // 当前线程的任务已经执行完毕,需修改SIZECTL,更新参与线程数量
                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
                        return;
                    finishing = advance = true;                                           // 扩容完成
                    i = n; // recheck before commit
                }
            }
            else if ((f = tabAt(tab, i)) == null)                                         // 对于空位置的处理,直接插入FWD
                advance = casTabAt(tab, i, null, fwd);
            else if ((fh = f.hash) == MOVED)                                              // 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;
                            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;
                                if ((ph & n) == 0)
                                    ln = new Node<K,V>(ph, pk, pv, ln);
                                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;
                        }
                    }
                }
            }
        }
    }

    // 线程通过cas申请到transfer任务,参与扩容即可
    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) {
                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;
    }

treeifyBin

将链表转为红黑树,只要当链表size>=8且map.size>=64时才触发转换,否则触发扩容
CHM中使用了TreeBin对象处理红黑树的操作细节,实现对TreeNode的管理。

    private final void treeifyBin(Node<K,V>[] tab, int index) {
        Node<K,V> b; int n, sc;
        if (tab != null) {
            // 数组长度小于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;
                        }
                        // 使用TreeBin容器对象组织TreeNode构建红黑树
                        setTabAt(tab, index, new TreeBin<K,V>(hd));
                    }
                }
            }
        }
    }

get

get获取元素:
①为普通节点(链表),则遍历判断获取
②e.hash<0,则可能为TreeBin或FWD,调用e.find()方法
对于FWD对象,会进入NextTable中查找对应元素的值(扩容不影响get操作)

    public V get(Object key) {
        //tab:当前散列表
        //e:当前元素
        //p:目标节点
        //n:table数组长度
        //eh:e的hash值
        //ek:当前元素的key
        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
        //高十六位参与运算
        int h = spread(key.hashCode());
        //条件一:true:table不为空
        //条件二:key对应的hash值对应在数组的下标位置为空
        if ((tab = table) != null && (n = tab.length) > 0 &&
                (e = tabAt(tab, (n - 1) & h)) != null) {
            //CASE1:当前桶位的头节点hash值与查找结点hash值一致
            if ((eh = e.hash) == h) {
                //hash值一致,判断key是否一致,一致的话就返回对应数据
                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
                    return e.val;
            }
            //CASE2:-1代表当前元素已经被迁移走了
            //		-2代表当前元素TreeBin节点,使用find方法进行查询
            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;
    }

        // FWD中的find()方法,将会指引到新的table中进行查询
    static final class ForwardingNode<K,V> extends Node<K,V> {
        final Node<K,V>[] nextTable;
        ForwardingNode(Node<K,V>[] tab) {
            super(MOVED, null, null, null);
            this.nextTable = tab;
        }

        Node<K,V> find(int h, Object k) {
            // loop to avoid arbitrarily deep recursion on forwarding nodes
            outer: for (Node<K,V>[] tab = nextTable;;) {
                //e:表示在扩容创建的新表使用寻址算法得到的桶位头接待你
                //n:表示新表的长度
                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 (;;) {
                    //eh:新扩容表指定桶位节点的hash值
                    //ek:新扩容表指定桶位节点的key值
                    int eh; K ek;
                    // 条件成立,表示当前元素是查找的元素,返回即可
                    if ((eh = e.hash) == h &&
                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
                        return e;
                    //eh < 0   1.treeBin  2.FWD节点
                    if (eh < 0) {
                        if (e instanceof ForwardingNode) {
                            tab = ((ForwardingNode<K,V>)e).nextTable;
                            continue outer;
                        }
                        //treeBin的find
                        else
                            return e.find(h, k);
                    }
                    //链表结构 - 查找到最后一个节点
                    if ((e = e.next) == null)
                        return null;
                }
            }
        }
    }

推荐参考:ConcurrentHashMap


欢迎疑问、期待评论、感谢指点 -- kiqi,愿同您为友

-- 星河有灿灿,愿与之辉

原文地址:https://www.cnblogs.com/kiqi/p/14420577.html