在我的配置类中,我有一个用于FixedThreadPool的bean
@Bean
public ExecutorService fixedThreadPool() {
return Executors.newFixedThreadPool(200);
}
我稍后会在某堂课上自动装配它。在我研究的过程中,我发现这是关闭执行程序服务的最佳方法,根据java docs
void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
但我还发现spring会为你做这件事,如果你的ExecutorService是一个bean。我的问题是——这是真的吗?如果是的话,那肯定会更容易,因为我不确定将上面提到的代码放在哪里。如果我把它放在我使用这个bean的类中,我将来可能会在其他一些类中使用这个bean,这似乎是错误的。
是的,Spring 会为你做到这一点,但只有当容器关闭时,它才会销毁 bean,你可能希望在处理完所有任务后关闭执行服务。您可以创建 ExecutorService bean 作为原型,但由于 spring 不会完全管理其生命周期,您必须在任务完成后自己调用其 destroy 方法。
默认情况下,Spring仅调用ExecutorService的shutdown方法。 调用destroy方法后,spring认为它的工作已经完成。 shutdown 是一个异步方法,不会阻塞 destroy 方法。它不会等待 ExecutorService 完成其工作。 之后,Spring 应用程序将直接停止,而不等待队列中的任务完成。 有几种方法可以避免这个问题。
我们可以保留默认行为并使用队列大小为零的线程池。它将等待线程中已有的任务。
我们可以使用 Queue 并定义我们自己的 destroy 方法,包含以下代码。
自定义销毁方法
void customDestroyMethod() {
super.shutdown();
this.awaitTermination(max_time_you_can_wait_ExecutorService, TimeUnit.SECONDS);
}