同时将某些内容放入地图时发生NullPointerException

时间:2018-10-24 09:00:31

标签: java multithreading

我通过运行多个sql使用映射来存储数据。最近,我想提高搜索效率,因此我创建了一些线程,每个线程对应每个sql,然后将查询结果同时放入映射中。 运行代码时,我得到NullPointerExceptions,但不知道为什么。 这是我的代码:

public Map<String, Object> getIndexStatistics(final Integer themeId, final String time, final Integer projectId) {
    final Map<String, Object> res = new ConcurrentHashMap<>();
    final AtomicInteger ai = new AtomicInteger(0);
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part1", getSumVolume(themeId, time, true, projectId));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part2", getVolumeTendency(themeId, time, projectId));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part3", getPlatformDistribution(themeId, time, projectId));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part4", getFeelingProportion(themeId, time, projectId));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part5", getLocationDistribution(themeId, time, projectId));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part6", getPositiveCloudList(themeId, time));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part7", getNegativeCloudList(themeId, time));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part8", getLatestNegativeSentiments(themeId, time, projectId));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("part9", getHotRank(themeId, time, null, projectId));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("statistic_counts", getCountStatistics(time, themeId, projectId));
            ai.getAndIncrement();
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            res.put("region_counts", getRegionDistribution(themeId, time, projectId));
            ai.getAndIncrement();
        }
    }).start();
    while (true) {
        if (ai.get() == 11) {
            break;
        }
    }
    return res;
}

stacktrace:

java.lang.NullPointerException
    at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
    at java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006)
    at com.bolaa.main.service.impl.ProjectNameDataServiceImpl$7.run(ProjectNameDataServiceImpl.java:309)
    at java.lang.Thread.run(Thread.java:748)

顺便说一句,所有操作都是加法和推法。我需要使用AtomicInteger和ConcurrentHashMap吗?

1 个答案:

答案 0 :(得分:2)

您可以使用另一种类型的线程池[1]来提高功能的效率和测试结果。

问题是一个函数没有返回值[2]。 与Hashtable类似,但与HashMap不同,此类不允许将null用作键或值。[2]

您可以在所有线程中将function的参数更改为值123,并查找错误函数。或测试

final int errno= -1
if(getFeelingProportion(themeId, time, projectId)==null)
        res.put("part4", errno);
else
        res.put("part4", getFeelingProportion(themeId, time, projectId));
ai.getAndIncrement();

[1] https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html [2] https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html