gpt4 book ai didi

java - 同时将某些内容放入 map 时发生 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 01:51:14 25 4
gpt4 key购买 nike

我通过运行多个sql使用映射来存储数据。最近想提高搜索效率,所以创建了一些线程,每个线程对应每条sql,然后将查询结果同时放入map中。运行代码时,我得到 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;
}

堆栈跟踪:

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]来提高函数的效率和测试结果。

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

您可以在所有线程中将函数的参数更改为值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

关于java - 同时将某些内容放入 map 时发生 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52964909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com