我有Executor
需要在我关闭另一个Executor
之前终止,我正在考虑尝试实施等待通知策略,但通知必须来自{{ 1}},所以除非我将它子类化,否则我无法通知关机线程。是否可以选择对其进行子类化或旋转等待?
答案 0 :(得分:0)
Executor executor = Executors.newFixedThreadPool(YOUR_THREAD_COUNT);
exector.shutDown();
boolean shutdown = false;
try {
shutdown = executor.awaitTermination(YOUR_TIME_OUT, TimeUnit.MILLISECOND);
} catch (InterruptedException e) {
// handle the exception your way
}
if (!shutdown) {
LOG.error("Executor not shut down before time out.");
}
答案 1 :(得分:0)
我想也许你想要同步终止执行程序。我们始终可以使用变通方法来执行此操作:
//THE LOGIC BEFORE YOU WANT TO WAIT THE EXECUTOR
...
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
//THE LOGIC AFTER THE EXECUTOR TERMINATION
...
使用带有MAX_VALUE时间的awaitTermination方法。