Web application Development Note the issue of starting threads privately

Source: Internet
Author: User
Tags tomcat

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.