我有一段代码在 TimerTask 中每 5 秒运行一次,还使用 ExecutorService 为每个池跨越一个线程。
if(!alertingPools.isEmpty())
{
Object[] Pools = alertingPools.values().toArray();
executorService = Executors.newFixedThreadPool((Pools.length));
for (Object pool : Pools) {
executorService.execute(new DataAccumulation(timeStartSecData,
timeEndSec,pool, jsonArrayResult));
finally {
executorService.shutdown();
}
但有时会出现以下空指针异常:不确定为什么。还使用 ExecutorService 为每个池跨越一个线程
Exception in thread "Timer-0" java.lang.NullPointerException
at com.ebay.lbmongoservice.AlertsData$1.run(AlertsData.java:117)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)