在我的web应用程序中,我有3个线程,其中tomcat未能在重新加载时停止其中的2个线程。Tomcat无法停止webapp中的线程
严重:Web应用程序[/ myapp]似乎已经启动了一个名为[Thread-8]的线程,但未能阻止它。这很可能造成内存泄漏。 脉08,2013上午11时22分40秒org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
这将导致CPU使用率上升为每个重新加载。
这里是tomcat的无法停止一个线程:
一些在我的ServletContextListener实现的代码:
public void contextInitialized(ServletContextEvent event)
{
final UpdaterThread updaterThread = new UpdaterThread();
updaterThread.start();
event.getServletContext().setAttribute("updaterthread", updaterThread);
}
public void contextDestroyed(ServletContextEvent event)
{
UpdaterThread updaterThread = (UpdaterThread) event.getServletContext().getAttribute("updaterthread");
if (updaterThread != null)
{
updaterThread.stopUpdater();
updaterThread.interrupt();
updaterThread = null;
}
}
而且UpdaterThread的重要组成部分:
public class UpdaterThread extends Thread implements Runnable
{
private boolean alive = true;
@Override
public void run()
{
while(true)
{
try
{
while (alive)
{
doUpdate();
sleep(60*1000);
}
}
catch (InterruptedException ie) {}
catch (Exception e) {}
}
}
public void stopUpdater()
{
alive = false;
}
}
有没有人有任何想法,为什么这个线程不停止?有没有更好的方法来实现在特定时间执行某项工作的线程?
为什么你有两个'而(真)'和'而(活着)'?即使你将'alive'设置为false,它也会无休止地循环。 – NilsH 2013-05-08 10:32:01