我每 5 秒创建一次新线程,并使用Threadpool exector.
我确保线程正在关闭。但我越来越
Exception in thread "Timer-0" java.lang.OutOfMemoryError: unable to create new native thread.
这是因为我创建了许多线程而不是关闭它们吗?还是经常创建新的?
如果我在代码中做错了什么,有人可以告诉我吗?
public class API{
private MongoStoreExecutor executor = new MongoStoreExecutor(10,50);
private class MongoStoreExecutor extends ThreadPoolExecutor {
public MongoStoreExecutor(int queueSize, int maxThreadPoolSize) {
super(10, maxThreadPoolSize, 30, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(queueSize),
new ThreadPoolExecutor.CallerRunsPolicy());
}
}
public TimerTask alertingPoolsData() throws Exception, UnknownHostException {
TimeerTask task = new TimerTask(){
public void run(){
Pools = alertingPools.values().toArray();
List<Future<?>> tasks = new ArrayList<Future<?>>(Pools.length);
for (Object pool : Pools) {
tasks.add(executor.submit(new DataAccumulation(timeStartSecData,
timeEndSec,pool, jsonArrayResult,dataResult)));
}
for( Future<?> f: tasks) {
f.get(2 * 1000L, TimeUnit.MILLISECONDS);
}
}
} catch (Exception e) {
e.printStackTrace();
}
long interval = 5 * 1000L;
tm.scheduleAtFixedRate(task,(interval -
(System.currentTimeMillis() % interval)), interval);
return task;
}
}