我每5秒創建一個新線程並使用Threadpool exector.
我確保線程正在關閉。但我正在逐漸異常:java.lang.OutOfMemoryError,無法創建新的本地線程
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;
}
}
有人可以讓我知道是什麼造成這個問題? – user2325703
爲什麼要將'ThreadPoolExecutor'繼承爲只有一個構造函數調用?這是不好的做法,可能會導致錯誤的編程錯誤。 –
你有沒有檢查過有多少個正在運行的線程(可能使用'Thread.activeCount'?還有:*爲什麼*你會在5秒內啓動新線程?難道只能重新使用同一個池嗎? –