ConcurrentHashMap核心方法个人理解 jdk1.8

1、putVal() 底层:Node数组+链表+红黑树   发生hash冲突的时候,在该下标尾部添加新结点成为链表,链表的长度超过8则转换为红黑树


1)判断key和value为null则抛出空指针异常

2)通过二次hash计算出key对应的node节点数组下标

3)如果数组未被初始化,调用初始化的函数

4)如果对应下标的节点为null,表示该下标的没有节点,直接添加(这里使用了cas无锁机制,乐观锁的一种体现,大致思路是通过当前该位置的值V,现在为null,和执行添加新结点时候该下标的值进行比较C,如果V==C,表示还没有线程对该下标进行过更新操作,该下标对应的值仍为null,则可以把新节点添加到这个位置

5)如果当前hashmap正在扩容,则帮助扩容

6)发生了hash冲突

7)需要在添加前加锁,以防形成闭环

8)双重检测,确定当前的节点是对应下标的节点

9)如果该key已存在,则覆盖value值

10)遍历完链表未找到key,则添加新结点到链表末尾

11)如果是树节点类型,执行红黑树添加

12)如果链表长度大于8,则转换为红黑树


 final V putVal(K key, V value, boolean onlyIfAbsent) {
     1  if (key == null || value == null) throw new NullPointerException();
     2  int hash = spread(key.hashCode());
        int binCount = 0;
        for (Node<K,V>[] tab = table;;) {
            Node<K,V> f; int n, i, fh;
     3      if (tab == null || (n = tab.length) == 0)
                tab = initTable();
     4      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
            }
     5      else if ((fh = f.hash) == MOVED)
                tab = helpTransfer(tab, f);
     6      else {
                V oldVal = null;
     7           synchronized (f) {
     8              if (tabAt(tab, i) == f) {
                        if (fh >= 0) {
                            binCount = 1;
                            for (Node<K,V> e = f;; ++binCount) {
                                K ek;
     9                          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;
      10                        if ((e = e.next) == null) {
                                    pred.next = new Node<K,V>(hash, key,
                                                              value, null);
                                    break;
                                }
                            }
                        }
      11                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;
                            }
                        }
                    }
                }
      12        if (binCount != 0) {
                    if (binCount >= TREEIFY_THRESHOLD)
                        treeifyBin(tab, i);
                    if (oldVal != null)
                        return oldVal;
                    break;
                }
            }
        }
        addCount(1L, binCount);
        return null;
    }


2、get()方法

1)同样是计算出当前key在数组的下标

2)如果是空数组则返回null,必须是已经有值的Node数组,并且对应下标的值不能为null

3)下标存的值的key就是当前对应的key,直接返回value

4)树,查找

5)链表查找


public V get(Object key) {
        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
    1    int h = spread(key.hashCode());
    2   if ((tab = table) != null && (n = tab.length) > 0 &&
            (e = tabAt(tab, (n - 1) & h)) != null) {
    3       if ((eh = e.hash) == h) {
                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
                    return e.val;
            }
    4       else if (eh < 0)
                return (p = e.find(h, key)) != null ? p.val : null;
    5       while ((e = e.next) != null) {
                if (e.hash == h &&
                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
                    return e.val;
            }
        }
        return null;
    }



优秀理解:http://blog.csdn.net/u010887744/article/details/50637030

猜你喜欢

转载自blog.csdn.net/qq_31489805/article/details/78462664
今日推荐