MMP*_*Pgm 7 java java.util.concurrent
我有一个ThreadPoolExecutor,我向它提交任务.
private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
Run Code Online (Sandbox Code Playgroud)
此代码提交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);
}
}
Run Code Online (Sandbox Code Playgroud)
以下代码停止任务.
protected synchronized void stop(StrategyEntry entry) throws Exception {
for (Object future : futures) {
((Future<?>) future).cancel(true);
}
futures.clear();
threadPoolExecutor.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是:当我尝试停止任务时,我遇到以下异常:
从java.util.concurrent.ThreadPoolExecutor@216393fb拒绝的任务java.util.concurrent.FutureTask@3a475611 [已终止,池大小= 0,活动线程= 0,排队任务= 0,已完成任务= 1]
问题是你shutdown()在stop方法中的激励.如果您只想等待任务完成,请使用Future.get().当执行程序关闭时,无法再向其提交任务.
shutdown() 只应在实际要终止应用程序时使用.
| 归档时间: |
|
| 查看次数: |
12549 次 |
| 最近记录: |