【发布时间】:2023-03-02 21:32:01
【问题描述】:
我的应用程序中有一个线程池,我只想在应用程序停止时关闭它。我尝试了 3 种方法:
1.在上下文销毁时关机:
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("CALLING CONTEXT DESTROYED.");
PersistSigningExecutor.getService().shutdown();
}
2.在任何servlet的destroy方法中关闭:
@Override
public void destroy() {
super.destroy();
System.out.println("SHUTTING DOWN THREAD-POOLS");
PersistSigningExecutor.getService().shutdown();
}
3.在contextInitialised中添加关闭钩子
@Override
public void contextInitialized(ServletContextEvent sce) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
System.out.println("SHUTTING DOWN THREAD-POOLS");
PersistSigningExecutor.getService().shutdown();
}
});
}
但它们都不起作用。我没有看到任何打印声明。最后我得到日志说:
SEVERE: The web application [/app] appears to have started a thread named [pool-1-thread-1] but has failed to stop it. This is very likely to create a memory leak.
这意味着线程仍然没有关闭。当服务器关闭或应用程序未部署时,如何正确关闭该线程池?
【问题讨论】:
-
线程池 =
ExecutorService?
标签: java multithreading tomcat7 threadpool