gpt4 book ai didi

java - Tomcat 7 和 ScheduledExecutorService.shutdown

转载 作者:搜寻专家 更新时间:2023-10-31 20:05:11 24 4
gpt4 key购买 nike

我正在使用 ScheduledExecutorService 来运行计划线程。
我实现了 ServletContextListener.contextDestroyed 并调用了 ScheduledExecutorService.shutdownNowawaitTermination

这是一个例子:

@Override
public void contextDestroyed(ServletContextEvent servletcontextevent) {
pool.shutdownNow(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(50, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
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();
}
}


不过,我从 Tomcat 7 收到以下错误:

SEVERE: The web application [/servlet] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak.

这个日志可以忽略吗?还是我做错了什么?

谢谢

最佳答案

您确定此错误与您的线程池有关吗?根据线程名称“Timer-0”判断,它可能是由某种计时器启动的。

此外,shutdownNow() 应该返回仍在等待终止的任务列表(请参阅 JavaDoc)。如果列表不为空,您可以构建等待更多时间的逻辑。

关于java - Tomcat 7 和 ScheduledExecutorService.shutdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9930624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com