我有一个ExecutorService executor = Executors.newSingleThreadExecutor();我想在服务器关闭时停止.
我有一个实现ServletContextListener的类,它用@WebListener注释.
我在该课程中有两种方法:
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("ServletContextListener started");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
executor.shutdown();
executor.shutdownNow();
System.out.println("ServletContextListener destroyed");
}
而且我看到它在它们应该的时候打印出它们中的内容,但是当我在intelij中按下一次停止按钮时,我得到:
SEVERE: The web application [] appears to have started a thread named [pool-2-thread-1] but has Failed to stop it. This is very likely to create a memory leak.
在它打印出ServletContextListener之后就被销毁了.
我需要再次按下停止按钮才能完全停止它.
为什么它不会关闭ExecutorService,即使它到达executor.shutdown();?我究竟做错了什么?
PS:这是我唯一的ExecutorService,没有其他线程由我制作.
EDIT2:
执行程序服务是单例类中的一个字段,它使用类初始化:
private ExecutorService executor = Executors.newSingleThreadExecutor();
这是类的初始化方式(延迟初始化):
public static RoomsManager getRoomsManager(ServletContext servletContext) {
if (servletContext.getAttribute(MANAGER_GAMES_ATTRIBUTE_NAME) == null) {
servletContext.setAttribute(MANAGER_GAMES_ATTRIBUTE_NAME,new RoomsManager());
}
return (RoomsManager)servletContext.getAttribute(MANAGER_GAMES_ATTRIBUTE_NAME);
}
并注释如下:
@WebListener
public class RoomsManager implements ServletContextListener {
停止按钮是intelij IDEA中播放和调试按钮附近的红色方块.
最佳答案
原文链接:https://www.f2er.com/java/437349.html