ConcurrentHashMap 一些锁事

  • 1.8版本比之前版本的优化之处
    1、存储元素的节点类型由Entry  变成 Node(多了next指针)  有利于链表改成树存储
    
    2、取消Segment, 由CAS + synchronized来解决并发问题
    
    3、另外,在其他方面也有一些小的改进,比如新增字段 transient volatile CounterCell[] counterCells; 
    可方便的计算hashmap中所有元素的个数,性能大大优于jdk1.7中的size()方法(遍历segment数组进行计数)
  • put 方法
    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;
            for (Node<K,V>[] tab = table;;) {
                Node<K,V> f; int n, i, fh;
                if (tab == null || (n = tab.length) == 0)
                    tab = initTable();
                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
                }
                else if ((fh = f.hash) == MOVED)
                    tab = helpTransfer(tab, f);
                else {
                    V oldVal = null;
                    synchronized (f) {
                        if (tabAt(tab, i) == f) {
                            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) {
                        if (binCount >= TREEIFY_THRESHOLD)
                            treeifyBin(tab, i);
                        if (oldVal != null)
                            return oldVal;
                        break;
                    }
                }
            }
            addCount(1L, binCount);
            return null;
        }
    
    
    首先根据key 获取hash,然后根据hash找到对应的hash数组位置
    
    1、如果哈希表还未初始化,那么初始化它
    
    2、检测到桶结点是 ForwardingNode 类型,协助扩容
    
    3、判断该hash数组位置是否有头节点,没有的话则通过cas进行头节点赋值
    
       如果该hash数组位置有头节点,则sychronized头节点,往尾结点后面添加新元素(或者key相等的时候覆盖)  
     也可能是数结构,如果是树则需要进行数节点操作
    
    4、添加完节点,判断某hash数组的桶内的数目大于 8 ,大于等于8则需要进行链表转红黑树
     
  •  get   不涉及并发情况
    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;
                }
                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;
        }
  • 3、clear 方法将删除整张哈希表中所有的键值对,删除操作也是一个桶一个桶的进行删除。
    /**
         * Removes all of the mappings from this map.
         */
        public void clear() {
            long delta = 0L; // negative number of deletions
            int i = 0;
            Node<K,V>[] tab = table;
            while (tab != null && i < tab.length) {
                int fh;
                Node<K,V> f = tabAt(tab, i);
                if (f == null)
                    ++i;
                else if ((fh = f.hash) == MOVED) {
                    tab = helpTransfer(tab, f);
                    i = 0; // restart
                }
                else {
                    synchronized (f) {
                        if (tabAt(tab, i) == f) {
                            Node<K,V> p = (fh >= 0 ? f :
                                           (f instanceof TreeBin) ?
                                           ((TreeBin<K,V>)f).first : null);
                            while (p != null) {
                                --delta;
                                p = p.next;
                            }
                            setTabAt(tab, i++, null);
                        }
                    }
                }
            }
            if (delta != 0L)
                addCount(delta, -1);
        }

猜你喜欢

转载自blog.csdn.net/ma_ru_long/article/details/106681560