zoukankan      html  css  js  c++  java
  • java源码ConcurrentHashMap分析1

    ConcurrentHashMap源码分析 版本jdk8 摈弃了jdk7之前的segement段锁:

    首先分析一下put方法,大致的流程就是首先对key取hash函数 判断是否first节点是否存在 不存在则 cas更新,存在  判断是否是forward节点,如果是则帮助扩容,否则锁住first节点 然后循环遍历链表判断事够key.equals()跟hash是否相等,

    相等则直接替换旧值,如果遍历到链表next==null,则直接新建一个node,然后next指向他,最后在调用addCount()方法并发统计跟扩容

        /** Implementation for put and putIfAbsent */
        final V putVal(K key, V value, boolean onlyIfAbsent) {
            // 可见concurrentHashMap不支持key 跟 value为空
            if (key == null || value == null) throw new NullPointerException();
            // 通过hash函数得到hash值
            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)
                    tab = initTable();
                // hash & (n-1) 得到数组索引值, 为空
                else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
                    //  cas更新头节点 这边可能多线程更新头节点,其他线程更新失败后则开始继续循环
                    if (casTabAt(tab, i, null,
                                 new Node<K,V>(hash, key, value, null)))
                        break;                   // no lock when adding to empty bin
                }
                // 如果节点是forwoard节点 则开始帮助扩容
                else if ((fh = f.hash) == MOVED)
                    tab = helpTransfer(tab, f);
                else {
                    V oldVal = null;
                    // 锁住头节点
                    synchronized (f) {
                        // 再次判断i节点是否等于f 有可能被其他线程修改
                        if (tabAt(tab, i) == f) {
                            // -1 hash for forwarding nodes
                            // -2 hash for roots of trees
                            // -3 hash for transient reservations
                            if (fh >= 0) {
                                binCount = 1;
                                // 循环遍历链表
                                for (Node<K,V> e = f;; ++binCount) {
                                    K ek;
                                    // 如果hash相同 && key.equals(ek)) onlyIfAbsent=false 则直接替换旧值 跳出循环
                                    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 ;
                                    // 如果 fisrt节点没有next节点 直接新建节点设置next指向新节点  如果不为空则 将e指向next节点 继续循环
                                    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;
                    }
                }
            }
            // 并发统计count 扩容操作
        // 初始化表
        private final Node<K,V>[] initTable() {
            Node<K,V>[] tab; int sc;
            while ((tab = table) == null || tab.length == 0) {
                // sizeCtl 小于0 代表在初始化或扩容
                if ((sc = sizeCtl) < 0)
                    //此时放弃cpu
                    Thread.yield(); // lost initialization race; just spin
                // 并发修改sizeCtl为-1
                else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
                    // 初始化node数组  最后赋予sizeCtl=sc
                    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;
        }
    
    
    
     
  • 相关阅读:
    QT 开发小记
    linux c 时间函数
    ubuntu 16.04 登录后黑屏
    ubuntu 16.04 修正网卡与ifname对应关系
    HTML5 Shiv完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
    vue里面引入jq的方法
    如何禁用手机自带的输入法软键盘
    vue的首页渲染了两次的原因以及解决方法
    vue使用hightchats
    解决微信小程序使用switchTab跳转后页面不刷新的问题
  • 原文地址:https://www.cnblogs.com/1ssqq1lxr/p/9552864.html
Copyright © 2011-2022 走看看