《HashMap系列》第六集:ConcurrentHashMap的put()方法解析

一、put方法

/**
     * Maps the specified key to the specified value in this table.
     * Neither the key nor the value can be null.
     *
     * <p>The value can be retrieved by calling the {@code get} method
     * with a key that is equal to the original key.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with {@code key}, or
     *         {@code null} if there was no mapping for {@code key}
     * @throws NullPointerException if the specified key or value is null
     */
    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 都不能为空
        if (key == null || value == null) throw new NullPointerException();
        // 计算hash值
        int hash = spread(key.hashCode());
        // 用于记录相应链表的长度
        int binCount = 0;
        // 遍历数组
        for (ConcurrentHashMap.Node<K,V>[] tab = table;;) {
            ConcurrentHashMap.Node<K,V> f; int n, i, fh;
            // 如果数组不存在 或数组长度为0 初始化表
            if (tab == null || (n = tab.length) == 0)
                tab = initTable();
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {// 如果数组当前位置为null
                // 用CAS操作将值放入
                if (casTabAt(tab, i, null,
                        new ConcurrentHashMap.Node<K,V>(hash, key, value, null)))
                    break;                   // no lock when adding to empty bin
            }
            // 如果table正在扩容 则得到扩容后的table 然后在重新开始一个循环
            else if ((fh = f.hash) == MOVED)
                tab = helpTransfer(tab, f);
            else {
                // 到这里说明找到了key hash后对应的table,并且table上有其他node的存在
                V oldVal = null;
                // 把这个找到的node加上同步锁,防止并发出现的问题,如果其他key put进来的时候也对应这个tab则堵塞在这里
                synchronized (f) {
                    // 再次用cas确认索引i上的table为我们找到的node,如果不是的话则这个node被修改,直接释放锁进入下一个循环
                    if (tabAt(tab, i) == f) {
                        // 如果目标table的第一个node的哈希值大于等于0,则是链式结构,走链表查找,反之走红黑树查找
                        if (fh >= 0) {
                            // 标记点
                            binCount = 1;
                            // 如果遍历元素的哈希值与需要插入目标key的哈希值相同,并且值也相同,则插入的是重复key的元素
                            for (ConcurrentHashMap.Node<K,V> e = f;; ++binCount) {
                                K ek;
                                if (e.hash == hash &&
                                        ((ek = e.key) == key ||
                                                (ek != null && key.equals(ek)))) {
                                    oldVal = e.val;
                                    // 如果onlyIfAbsent为false的话,则替换为新value,否则不修改
                                    if (!onlyIfAbsent)
                                        e.val = value;
                                    break;
                                }
                                // 循环直到最后一个node节点的key都不是我们想要插入的key
                                ConcurrentHashMap.Node<K,V> pred = e;
                                if ((e = e.next) == null) {
                                    //在尾部添加一个新节点,break循环
                                    pred.next = new ConcurrentHashMap.Node<K,V>(hash, key,
                                            value, null);
                                    break;
                                }
                            }
                        }
                        //该节点属于红黑树的子节点,进行树操作
                        else if (f instanceof ConcurrentHashMap.TreeBin) {
                            ConcurrentHashMap.Node<K,V> p;
                            binCount = 2;
                            if ((p = ((ConcurrentHashMap.TreeBin<K,V>)f).putTreeVal(hash, key,
                                    value)) != null) {
                                oldVal = p.val;
                                if (!onlyIfAbsent)
                                    p.val = value;
                            }
                        }
                    }
                }
                // 如果node节点不为0
                if (binCount != 0) {
                    // 如果node大于或者等于8,则转为红黑树
                    if (binCount >= TREEIFY_THRESHOLD)
                        treeifyBin(tab, i);
                    if (oldVal != null)
                        return oldVal;
                    break;
                }
            }
        }
        //进行扩容判断
        addCount(1L, binCount);
        return null;
    }

 二、initTable() 初始化表

/**
     * Table initialization and resizing control.  When negative, the
     * table is being initialized or resized: -1 for initialization,
     * else -(1 + the number of active resizing threads).  Otherwise,
     * when table is null, holds the initial table size to use upon
     * creation, or 0 for default. After initialization, holds the
     * next element count value upon which to resize the table.
     */
    private transient volatile int sizeCtl;

    /**
     * Initializes table, using the size recorded in sizeCtl.
     */
    private final ConcurrentHashMap.Node<K,V>[] (() {
        ConcurrentHashMap.Node<K,V>[] tab; int sc;
        // 通过循环判断
        while ((tab = table) == null || tab.length == 0) {
            // sizeCtl小于0 判断是否有线程占用 如果有 则让出cpu,进入就绪状态 默认值为0 -1指的是正在初始化
            if ((sc = sizeCtl) < 0)
                Thread.yield(); // lost initialization race; just spin
            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {//CAS操作 将sc的值修改为1
                try {
                    if ((tab = table) == null || tab.length == 0) {
                        // 如果 sizeCtl>0 初始化大小为sizeCtl,否则初始化大小为16
                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
                        @SuppressWarnings("unchecked")
                        ConcurrentHashMap.Node<K,V>[] nt = (ConcurrentHashMap.Node<K,V>[])new ConcurrentHashMap.Node<?,?>[n];
                        table = tab = nt;
                        //sc赋值,如果n为16,则sc = 16-16/4 = 12
                        sc = n - (n >>> 2);
                    }
                } finally {
                    // 赋值给sizeCtl,初始化结束,sizeCtl的值>0
                    sizeCtl = sc;
                }
                break;
            }
        }
        return tab;
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HashMapConcurrentHashMap的区别在于线程安全性和并发性能。 HashMap是非线程安全的,如果多个线程同时对其进行读写操作,可能会导致数据不一致或者抛出异常。而ConcurrentHashMap是线程安全的,可以在多线程环境下使用而不需要额外的同步措施。 ConcurrentHashMap保证线程安全的方式是通过分段锁技术。它将数据分成多个段(Segment),每个段都有自己的锁。当一个线程对某个段进行操作时,只需获取该段的锁,不会影响其他段的访问。这样,不同的线程可以同时操作不同的段,提高了并发性能。而HashMap没有这种机制,所有线程都需要竞争同一个锁,因此并发性能较低。 此外,ConcurrentHashMap还使用了CAS(Compare and Swap)操作和volatile关键字来保证数据的一致性。CAS操作是一种无锁的原子操作,它可以确保对变量的读写操作是原子的。volatile关键字可以保证变量的可见性,即当一个线程修改了变量的值后,其他线程可以立即看到这个修改。 综上所述,ConcurrentHashMap相较于HashMap,具有更好的线程安全性和并发性能,适用于高并发环境下的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [阿里面试题:ConcurrentHashMap为什么是线程安全的?](https://download.csdn.net/download/weixin_38717171/14854002)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [HashMapconcurrentHashMap的区别](https://blog.csdn.net/qq_42949615/article/details/124109063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [HashMapConcurrentHashMap的区别,ConcurrentHashMap线程安全吗,ConcurrentHashMap如何保证线程安全?](https://blog.csdn.net/YANG_Gang2017/article/details/80218091)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为人师表好少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值