我有一個ThreadPoolExecutor
,我向它提交了一個任務。從ThreadPoolExecutor拒絕的未來任務
private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
此代碼提交Runnable
到ThreadPoolExecutor
。
protected void waitAndSweep(final String symbol) {
runnable = new Runnable() {
public void run() { /* irrelevant code */ }
};
try {
Future<?> self = threadPoolExecutor.submit(runnable);
futures.add(self);
} catch (RejectedExecutionException re) {
/* this exception will be thrown when wait and sweep is called more than twice.
* threadPoolExecutor can have one running task and one waiting task.
*/
} catch (Exception e) {
logEvent(StrategyEntry.ERROR, "waitAndSweep", symbol, "Exception caught...", e);
}
}
以下代碼將停止該任務。
protected synchronized void stop(StrategyEntry entry) throws Exception {
for (Object future : futures) {
((Future<?>) future).cancel(true);
}
futures.clear();
threadPoolExecutor.shutdown();
}
這裏的問題是:當我試圖停止任務,我得到以下異常:
任務[email protected]從java.util.concurrent的拒絕。的ThreadPoolExecutor @ 216393fb [終止,池大小= 0,活動線程= 0,隊列任務= 0,完成的任務= 1]
你是否在這個執行器的任何地方調用'shutdown()'? – RAnders00
是在停止方法 – MMPgm
您正在創建容量爲1的後備隊列 - 是否存在其他一些任務? –