HashMap多线程数据丢失问题

2019-11-26  本文已影响0人  康俊1024

问题代码

        Map<String, JSONArray> auditStreamInfoMapping = new HashMap<>();
        //开启线程池
        Integer totalArraySize = tasks.size();
        Integer suggestedThreadNum = Runtime.getRuntime().availableProcessors() * 2 + 1; //建议最大线程数量,IO密集型:cpu核心数*2+1
        ExecutorService threadPool = Executors.newFixedThreadPool((totalArraySize < suggestedThreadNum) ? totalArraySize : suggestedThreadNum);
        final CountDownLatch countDownLatch = new CountDownLatch(totalArraySize); //创建CountDownLatch

        for (Task task : tasks) {
            threadPool.execute(new Runnable() { //执行线程
                @Override
                public void run() {
                    try {
                        auditStreamInfoMapping.put(task.getTask_id(), JSONArray.parseArray(task.getAudit_stream()));
                    } catch (Exception e) {
                        log.error(e.getMessage());
                    } finally {
                        countDownLatch.countDown(); //CountDownLatch的值减1
                    }
                }
            });
        }
        countDownLatch.await(); //主线程挂起,直到CountDownLatch的值为0
        threadPool.shutdown(); //关闭线程池

问题分析

HashMap源码

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)                   // 如果没有hash碰撞则直接插入元素
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

总结

  1. 在jdk1.7中,在多线程环境下,扩容时会造成环形链或数据丢失。
  2. 在jdk1.8中,在多线程环境下,会发生数据覆盖的情况。
上一篇 下一篇

猜你喜欢

热点阅读