ConcurrentHashMap源码分析

1.什么是ConcurrentHashMap?

ConcurrentHashMap是一个线程安全的Map类。

1.2ConcurrentHashMap是如何实现线程安全的?

ConcurrentHashMap通过cas、volatile、以及分段synchronized等逻辑实现线程安全操作。

2.ConcurrentHashMap#put源码分析

//put方法调用的putVal方法
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) {
    //判空,若key或value有一个为null则报空指针异常
        if (key == null || value == null) throw new NullPointerException();
        //根据key的方法获取hash值 spread方法见下分析
        int hash = spread(key.hashCode());
        int binCount = 0;
        for (Node<K,V>[] tab = table;;) {
            Node<K,V> f; int n, i, fh;
            //如果第一次进入tab为null时,则初始化tab
            if (tab == null || (n = tab.length) == 0)
            //详细分析见下
                tab = initTable();
                //如果非第一次进入,此处通过tabAt方法获取hash值对应的下标(见tabAt详解)
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
            	//若当前元素为null,则构造一个Node节点通过casTabAt进行设置操作
                if (casTabAt(tab, i, null,
                             new Node<K,V>(hash, key, value, null)))
                    break;                   // no lock when adding to empty bin
            }
            //如果hash为MOVED标识表示当前的节点处于移动状态,则此时会帮助扩容操作
            else if ((fh = f.hash) == MOVED)
            //详细分析见下
                tab = helpTransfer(tab, f);
            else {
                V oldVal = null;
                //锁定当前Node数组元素
                synchronized (f) {
                    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)))) {
                                     //将当前节点的val值赋值给oldVal
                                    oldVal = e.val;
                                    if (!onlyIfAbsent)
                                    //将新的值赋给e.val
                                        e.val = value;
                                    break;
                                }
                                Node<K,V> pred = e;
                                //如果需要put的值与当前元素hash值相等,但是key不相同时,找到当前元素最后一个节点,
                                //并将next节点指向需要put的节点
                                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) {
                //如果节点大于TREEIFY_THRESHOLD(8) 则构造红黑树
                    if (binCount >= TREEIFY_THRESHOLD)
                        treeifyBin(tab, i);
                    if (oldVal != null)
                        return oldVal;
                    break;
                }
            }
        }
        //计数 分析见下
        addCount(1L, binCount);
        return null;
    }

put方法的大致流程如注释所示,上面的代码细节分析如下:

2.1 spread方法分析

static final int spread(int h) {
        return (h ^ (h >>> 16)) & HASH_BITS;
    }

假设此处的key为"test",即h为"test"的hascode=3556498。
二进制位:

0000 0000 0011 0110 0100 0100 1001 0010

h(3556498)>>>16(将高16位移动到低16位,并将高16位补零):

0000 0000 0000 0000 0000 0000 0011 0110

(h ^ (h >>> 16))(原hascode高16位值不变,低16位与高16位左异或运算(对后续hash槽分配更均匀)):

	0000 0000 0011 0110 0100 0100 1001 0010
^	0000 0000 0000 0000 0000 0000 0011 0110
______________________________________________
	0000 0000 0011 0110 0100 0100 1010 0100

(h ^ (h >>> 16)) & HASH_BITS(0x7fffffff) = 3556516(保证为正数,负数为红黑树节点):

	0000 0000 0011 0110 0100 0100 1010 0100
&	0111 1111 1111 1111 1111 1111 1111 1111 
————————————————————————————————————————————————
	0000 0000 0011 0110 0100 0100 1010 0100

2.2 initTable()方法分析

 private final Node<K,V>[] initTable() {
        Node<K,V>[] tab; int sc;
        while ((tab = table) == null || tab.length == 0) {
        	//sizeCtl默认为0 当sizeCtl为1时表示有线程正在初始化操作(见下方代码),当有线程初始化时则主动让出cpu执行权
            if ((sc = sizeCtl) < 0)
                Thread.yield(); // lost initialization race; just spin
                //cas操作改变sizeCtl为-1,表示占位,有线程正在执行init操作
            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
                try {
                    if ((tab = table) == null || tab.length == 0) {
                    	//DEFAULT_CAPACITY = 16
                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
                        @SuppressWarnings("unchecked")
                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
                        table = tab = nt;
                        //16-16/4 = 12 == n*0.7 进行百分之75的计算为扩容阈值
                        sc = n - (n >>> 2);
                    }
                } finally {
                	//执行完后将sizeCtl赋值为扩容阈值
                    sizeCtl = sc;
                }
                break;
            }
        }
        return tab;
    }

2.3 tabAt()方法

 @SuppressWarnings("unchecked")
    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
    }

为什么需要用getObjectVolatile?
1.volatile无法保证数组元素的可见性
2.假设当有对元素进行写操作时(之前为null),若无getObjectVolatile则可能导致此时获取的元素数据为null。本身应该进行挂链表操作的,此时则会对数组元素进行赋值操作(不使用casTabAt的前提)
3.使用getObjectVolatile可确保写操作happens-before读操作,即可避免上面的问题

2.4 casTabAt()

static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
                                        Node<K,V> c, Node<K,V> v) {
        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
    }

cas操作设置元素

2.5 addCount()

2.5.1 addCount方法思想

思考:
一、如何保证ConcurrentHashMap的size增加为线程安全的?
1.使用锁?
使用锁当然是ok的,但是因为在多线程的情况下,每put一个元素(key不重的情况下)都需要做一次size++操作,如果此时的并发请求量大,对于性能来说无非是噩梦般的存在。
2.使用cas?
同理,性能堪忧。当一个线程在cas操作时,其他的线程只能一直去循环。
二、那么jdk究竟是如何优化size的呢?
1. 当不存在并发增加size时,使用cas对baseCount进行++操作。
2. 若cas操作失败,则会创建或者扩容counterCells数组
3. 通过一个随机算法,分配一个counterCells元素,通过cas操作对元素的value值进行+1操作。
4.获取size = baseCount+各个counterCells元素的value值
* 这里引入的counterCells,有点像分片的概念,多个线程并发进行++操作时,则将多个请求分流到不同的元素中进行cas++操作。

2.5.2 addCount()源码分析
private final void addCount(long x, int check) {
        CounterCell[] as; long b, s;
        //如果counterCells不为null,或者尝试去对baseCount进行++操作失败
        if ((as = counterCells) != null ||
            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
            CounterCell a; long v; int m;
            boolean uncontended = true;
            //如果counterCells为null 或者as.length小于1或者counterCells[随机数取模]==null
            //或者cas设置counterCells[随机数取模]失败进入下面的方法
            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;
            s = sumCount();
        }
        if (check >= 0) {
            Node<K,V>[] tab, nt; int n, sc;
            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
                   (n = tab.length) < MAXIMUM_CAPACITY) {
                int rs = resizeStamp(n);
                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);
                }
                else if (U.compareAndSwapInt(this, SIZECTL, sc,
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
                    transfer(tab, null);
                s = sumCount();
            }
        }
    }
2.5.3 fullAddCount()
//先看最下面的init方法
 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) {
            //如果获取的随机元素为null时
                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)) {
                            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;
                }
                //当cas操作失败时,会有一次重试的机会
                else if (!wasUncontended)       // CAS already known to fail
                    wasUncontended = true;      // Continue after rehash
                    //进行cas操作设置数组元素++
                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
                    break;
                    //若其他线程对counterCells 进行了扩容操作,则退出当前的if else操作,重新开始判断
                else if (counterCells != as || n >= NCPU)
                    collide = false;            // At max size or stale
                else if (!collide)
                    collide = true;
                    //若counterCells中的元素都以初始化,并且多次cas操作失败,则进行扩容操作
                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
                }
                h = ThreadLocalRandom.advanceProbe(h);
            }
            //1、init方法,将cellbusy设置为1 1标识为占位
            else if (cellsBusy == 0 && counterCells == as &&
                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
                boolean init = false;
                try {                           // Initialize table
                    if (counterCells == as) {
                    	//出事话一个长度为2的CounterCell数组
                        CounterCell[] rs = new CounterCell[2];
                        //随机一个数组元素,实例化并且将元素的value值设置为1
                        rs[h & 1] = new CounterCell(x);
                        //counterCell赋值
                        counterCells = rs;
                        init = true;
                    }
                } finally {
                    cellsBusy = 0;
                }
                if (init)
                    break;
            }
            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
                break;                          // Fall back on using base
        }
    }
2.5.4 addCount下半段代码分析
 if (check >= 0) {
            Node<K,V>[] tab, nt; int n, sc;
            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
                   (n = tab.length) < MAXIMUM_CAPACITY) {
                   //获取一个resize时间戳,这里就不详细分析了
                   //高16位表示当前扩容的容度戳,后16位为线程数 为负数
                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;
                        //进行cas操作 将后16位的线程数+1
                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
                        transfer(tab, nt);
                }
                //设置sizeCtl+2表示有一个线程正在参与扩容 -1为占位符,所以从-2开始
                else if (U.compareAndSwapInt(this, SIZECTL, sc,
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
                    transfer(tab, null);
                s = sumCount();
            }
        }
      
2.5.4 transfer()
 private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
        int n = tab.length, stride;
        //计算每个cpu进行扩容的分配数,计算公式如下 
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
            stride = MIN_TRANSFER_STRIDE; // subdivide range
        //当扩容tab为null时
        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;
        //占位节点对应前面的helpTransfer
        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;
                }
                //假设此时为32-》64 则第一个线程进入时分配的坐标没16-31 第二个进入时分配的下标为0-15
                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
                }
            }
            //如果当前hash桶为null则将cas占位节点
            else if ((f = tabAt(tab, i)) == null)
                advance = casTabAt(tab, i, null, fwd);
            else if ((fh = f.hash) == MOVED)
                advance = true; // already processed
            else {
            //将当前的hash桶锁住(所谓的分段锁)
                synchronized (f) {
                    if (tabAt(tab, i) == f) {
                        Node<K,V> ln, hn;
                        if (fh >= 0) {
                        	//runBit==0不能迁移 反之进行迁移操作,下面一系列操作如下所示
                        	//简单的说0001 0000 (这是16) 如果hash值&16等于0表示不能迁移 
							//比如之前的后五位是00011(下标位3)&(10000)16 是不能迁移的(0&1==0) 为什么?
							//00011&11111(31)  结果还是3
							//10011&10000(16) 是可以迁移的(1&1==1)
							//10011&11111(31) 结果是3(0011)+16(10000) = 19 
                            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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值