ConcurrentHashMap

Jingxc大约 8 分钟java后端ConcurrentHashMapjava后端

ConcurrentHashMap

1. 构造器

ConcurrentHashMap的构造器和HashMap的构造器基本相同

2. put方法

ConcurrentHashMap键或者值为空会抛出空指针异常,put方法开始先求取hash

final V putVal(K key, V value, boolean onlyIfAbsent) {
    if (key == null || value == null) throw new NullPointerException();
    int hash = spread(key.hashCode());
    ...
}

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

提示

& HASH_BITS(二进制是31个1)就是为了让最终得到的哈希值始终是一个正数。

2.2 第一次添加键值对


第一次调用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
        }
        ...
    ...
    }
}

initTable方法

提示

sizeCtl:

  • -1:代表map数组正在初始化
  • 小于-1:代表正在扩容
  • 0:表示还没有初始化
  • 正数:若没有初始化,代表要初始化的长度;若已经初始化了,代表扩容的阈值 即临界值
private final Node<K,V>[] initTable() {
    Node<K,V>[] tab; int sc;
    while ((tab = table) == null || tab.length == 0) {
        if ((sc = sizeCtl) < 0)
            Thread.yield(); // lost initialization race; just spin
        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;
}

当多线程同时进行初始化的时候, U.compareAndSwapInt(this, SIZECTL, sc, -1) 线程会将sizeCtl比较并交换为-1,其他线程让出cpu片段直至初始化完成;

第一次添加键值对,遇到for(死循环)直接通过 casTabAt(tab, i, null,new Node<K,V>(hash, key, value, null)) 比较并赋值

2.3 第二次添加键值对


  • 在第二次添加数据时,和第一次基本一样,不会出现扩容的情景,但是有可能出现链表
  • 和第一次类似如果计算索引位子没有数据存在,则直接通过CAS赋值
  • 如果添加数据的索引位置存在数据,则转换为链表
final V putVal(K key, V value, boolean onlyIfAbsent) {
    // 如果键或者值为null,直接报空指针错误
    if (key == null || value == null) throw new NullPointerException();
    // 计算键的hash值`(h ^ (h >>> 16)) & HASH_BITS;`
    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) {
            // 如果几点位置原先不存在数据,通过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关键字进行多线程加锁,锁住整个槽位
            synchronized (f) {
                // 双端检索,结合for(死循环重新检测数据,数据变化则重新走流程)
                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)
                    // 如果是链表个树大于8,进行扩容或者转换为树结构
                    treeifyBin(tab, i);
                if (oldVal != null)
                    return oldVal;
                break;
            }
        }
    }
    addCount(1L, binCount);
    return null;
}

2.3 多次添加键值对


多次添加数据以后会遇到数组扩容或者树的转换,这就需要,查看 treeifyBin(tab, i); 方法了

private final void treeifyBin(Node<K,V>[] tab, int index) {
    Node<K,V> b; int n, sc;
    if (tab != null) {
        if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
            // 如果数组的长度小于64,直接进行扩容操做,不会新型红黑树的转换
            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));
                }
            }
        }
    }
}
private final void tryPresize(int size) {
    int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
        tableSizeFor(size + (size >>> 1) + 1);
    int sc;
    while ((sc = sizeCtl) >= 0) {
        Node<K,V>[] tab = table; int n;
        // 初始化数组
        if (tab == null || (n = tab.length) == 0) {
            n = (sc > c) ? sc : c;
            if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
                try {
                    if (table == tab) {
                        @SuppressWarnings("unchecked")
                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
                        table = nt;
                        // sc 表示扩容的时机
                        sc = n - (n >>> 2);
                    }
                } finally {
                    sizeCtl = sc;
                }
            }
        }
        else if (c <= sc || n >= MAXIMUM_CAPACITY)
            break;
        else if (tab == table) {
            int rs = resizeStamp(n);
            if (sc < 0) {
                Node<K,V>[] nt;
                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);
        }
    }
}

提示

sc = n - (n >>> 2); 扩容的临界值

扩容:

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容器并放入新容器
    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;
        //【第二步,划分槽位,帮助推进】
        //选择当前线程进行transfer的槽位,从最后一个槽位向前
        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;
                //设置下次扩容的临界点为 0.75*扩容容量
                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重置,在看下还有没有transferIndex
                //如果已经是唯一处理线程并且满足前置条件,为何需要检查下?
                i = n; // recheck before commit
            }
        }
        //【第四步,处理槽位】
        //如果当前槽中没有成员,用forwarding节点占位
        else if ((f = tabAt(tab, i)) == null)
            advance = casTabAt(tab, i, null, fwd);
        //如果当前槽中成员为forwarding节点,代表已经被处理过了    
        else if ((fh = f.hash) == MOVED)
            //处理下一个槽
            advance = true; // already processed
        else {
            //锁住槽位
            synchronized (f) {
                //double check
                if (tabAt(tab, i) == f) {
                    Node<K,V> ln, hn;
                    if (fh >= 0) {
                        //计算当前成员最高位
                        //runBit是0 or 1
                        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;
                            //查找最后重复的链,获得开始位置p,和重复的高位值runBit
                            if (b != runBit) {
                                runBit = b;
                                lastRun = p;
                            }
                        }
                        //如果从p开始后面高位全是0,那么就不需要移动到新槽中
                        if (runBit == 0) {
                            ln = lastRun;
                            hn = null;
                        }
                        //如果从p开始后面全是1,那么就需要移动到新槽中
                        else {
                            hn = lastRun;
                            ln = null;
                        }
                        //从链的头部一直遍历到p的位置(因为p以后高位都一样)
                        //为何需要提前找一部分重复?效率更高?这么处理是否有理论依据?
                        for (Node<K,V> p = f; p != lastRun; p = p.next) {
                            int ph = p.hash; K pk = p.key; V pv = p.val;
                            //高位为0放到旧槽位中
                            if ((ph & n) == 0)
                                ln = new Node<K,V>(ph, pk, pv, ln);
                            //高位为1放到新槽位中
                            else
                                hn = new Node<K,V>(ph, pk, pv, hn);
                        }
                        //将ln放到新容器的旧槽位中
                        setTabAt(nextTab, i, ln);
                        //将hn放到新容器的新槽位中
                        setTabAt(nextTab, i + n, hn);
                        //将老容器中的该节点设置为forwarding节点
                        setTabAt(tab, i, fwd);
                        //处理下一个槽位
                        advance = true;
                    }
                    //TreeBin的hash固定为-2,红黑树的调整
                    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;
                            }
                        }
                        //槽位里成员少于等于6,退化为链表
                        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;
                    }
                }
            }
        }
    }
}
上次编辑于:
贡献者: Jingxc