Signature: Canton Slippers
we know that static variables are classloader levels, and if the Web application stops, these static variables are also purged from the JVM. But the thread is at the JVM level, and if the user starts a thread in the Web app, The life cycle of this thread is not synchronized with the Web application. That is, even if the web app is stopped, the thread is still active. It is because of this very obscure problem that many experienced developers are not quite in favor of launching threads in a Web program privately . Example:
public class Test implements Servletcontextlistener {Private timer timer, public void contextdestroyed (Servletcontexteve NT arg0) {System.out.println ("Web application startup shutdown ..."); Timer.cancel (); public void contextinitialized (Servletcontextevent arg0) {System.out.println ("Web application Startup ..."); Timer = new timer (); TimerTask task = new Simpletimertask (); Timer.schedule (Task, 1000L, 5000L); }} class Simpletimertask extends TimerTask {private int count; public void Run () {System.out.println ((++count) + "execute Task ... "+ (new Date ())); } }
There is a configuration in the Web. xml file:
<listener> <listener-class> com.javaeye.lighter.Test </listener-class> </listener>
If this is the time to log into Tomcat's background, close the corresponding web App. At this point, we'll see the web app is closed on the Tomcat console, but the timer task continues to execute.
(Source: http://lighter.javaeye.com)