并发安全的集合ConcurrentHashMap

本堂课目标

  • ConcurrentHashMap的存储结构
  • ConcurrentHashMap一些重要的设计思想
    • 并发扩容
    • 高低位迁移
    • 分段锁(*)
    • 红黑树
    • 链表

为什么要用ConcurrentHashMap

HashMap -> 非线程安全的
HashTable synchronized(偏向锁、轻量级锁(CAS))
性能 -> 安全性之间做好平衡

ConcurrentHashMap的使用

java - 8 lambda(一种简化,语法糖)

  • computeIfAbsent
  • computeIfPresent
  • compute(computeIfAbsent和computeIfPresent两者的结合)
  • merge(合并数据)

ConcurrentHashMap它的存储结构和实现.

JDK1.8
JDK1.7 segment 分段锁。锁的粒度较大。
红黑树的引入, 时间复杂度O(n) -> O(logn)
.在这里插入图片描述

CHM的源码

transient volatile Node<K,V>[] table;
    final V putVal(K key, V value, boolean onlyIfAbsent) {
        if (key == null || value == null) throw new NullPointerException();
        int hash = spread(key.hashCode());
        int binCount = 0;
//自旋(;;) {cas}
        for (Node<K,V>[] tab = table;;) {
            Node<K,V> f; int n, i, fh;
//如果tab为空,说明还没有初始化.
            if (tab == null || (n = tab.length) == 0)
                tab = initTable(); //初始化完成后,进入到下一次循环
//(n - 1) & hash) -> 0-15 ->计算数组下标位置.
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
//如果当前的node的位置为空,直接存储到该位置.
//通过cas来保证原子性.
                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) { //锁住当前的node节点,避免线程安全问题
                    if (tabAt(tab, i) == f) { //重新判断()
//针对链表来处理
                        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;
        }
//如果不存在,则把当前的key/value添加到链表中.
        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,则会调用treeifyBin方法
        if (binCount >= TREEIFY_THRESHOLD)
        treeifyBin(tab, i);
        if (oldVal != null)
        return oldVal;
        break;
        }
        }
        }
        addCount(1L, binCount);
        return null;
        }

initTable();

   private final Node<K,V>[] initTable() {
        Node<K,V>[] tab; int sc;
//只要tab没有初始化,就不断循环直到初始完成
        while ((tab = table) == null || tab.length == 0) {
            if ((sc = sizeCtl) < 0)
                Thread.yield(); // lost initialization race; just spin
//通过cas自旋(通过CAS来占用一个锁的标记)
            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;
        }

treeifyBin

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;
                        }
                        setTabAt(tab, index, new TreeBin<K,V>(hd));
                    }
                }
            }
        }
    }

tryPresize

该方法用来实现扩容,

  • 多线程并发扩容(允许多个线程来协助扩容)
  • 扩容的本质
    • 多线程并发扩容(允许多个线程来协助扩容)
    • 扩容的本质

必须要有一个地方去记录,在当前扩容范围内,有多少个线程参与数据的迁移工作. 必须要保证所有的
线程完成了迁移的动作,才能够表示扩容完成。

resizeStamp

static final int resizeStamp(int n) {
return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
}

0000 0000 0000 0000 0000 0000 0001 0000 -> 27位
0000 0000 0000 0000 1000 0000 0001 1011
resizeStamp返回的数: 1000 0000 0001 1011 0000 0000 0000 0000
rs << RESIZE_STAMP_SHIFT) + 2 ,二进制左移16位+2
1000 0000 0001 1011 0000 0000 0000 0010 ->表示当前有一个线程来扩容。
高位16表示当前的扩容标记, 保证唯一性.
低16位表示当前扩容的线程数量.

transfer

如何实现多个线程对同一个容器做数据迁移.
实现数据转移。

  • 数据的迁移
    • 需要计算当前线程的数据迁移空间。(任务拆分)
    • 创建一个新的数组(容量为扩容后的大小)
    • 实现数据的转移。
      • 如果是红黑树
        • 数据迁移后,不满足红黑树的条件,则红黑树转链表
      • 如果是链表
      • 高低位迁移
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
        int n = tab.length, stride;
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
            stride = MIN_TRANSFER_STRIDE; // subdivide range
        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 = 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 = 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
                }
            }
            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;
                            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;
                        }
                    }
                }
            }
        }
    }

size() 怎么统计?

红黑树

第二节、扩容

  • 创建一个新的数组
  • 对老的数组的数据进行迁移
  • 多线程辅助扩容(针对老的数据,通过多个线程并行来执行数据的迁移过程)
    • 记录当前的线程数量(sizeCtl)
    • 当每个线程完成数据迁移之后,退出的时候,要减掉协助扩容的线程数量
  • sizeStamp -> 扩容戳
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
        int n = tab.length, stride;
        //计算每个线程处理的数据的区间大小,最小是16。
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
            stride = MIN_TRANSFER_STRIDE; // subdivide range
            //表示扩容之后的数组,在原来的基础上扩大两倍。
        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 = n;
        }
        //transferIndex = old table[] 的长度。
        int nextn = nextTab.length;
        //用来表示已经迁移完的状态,也就是说,如果某个old数组的节点完成了迁移,则需要更改成fwd
        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;
                }
                //假设数组长度是32,
//第一次 [16(nextBound),31(i)]
//第二次 [0,15]
            }
            //是否扩容结束
            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
                }
            }
            //说明当前数组位置为空
            else if ((f = tabAt(tab, i)) == null)
            //直接改成fwd -> 表示迁移完成.
                advance = casTabAt(tab, i, null, fwd);
                //判断是否已经被处理过了,如果是,则进入下一次
区间遍历.
            else if ((fh = f.hash) == MOVED)
                advance = true; // already processed
            else {
             //保证迁移过程中,其他线程调用put()方法时,必须要等待。
//下面的内容的猜测:
//* 针对不同类型的节点,做不同的处理
//* 链表
//* 红黑树
                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;
                        }
                    }
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值