在我的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