Thread does not recycle after web app is closed

Source: Internet
Author: User

Today, in the "SPRING3.0 Enterprise application Development Practice" in the Web application in the launch and shutdown of the scheduler, there is a passage:

Static variables are ClassLoader level, 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 thread's life cycle does not stay in sync with the Web application. That is, even if the web app is stopped, the thread is still active.

So in order to really verify the above conclusions, the actual practice of the example of the book

One Configuration Tomcat App Management page

1. After starting Tomcat, access the http://localhost:8080/via the Web, the following page appears

2. Click the Manager app to open the Enter user name password pop-up window

3. Because you do not know the user name password, directly canceled, an error message prompts the new page

4. From the new page above can be seen, meaning that if you do not change the configuration file, directly open the Conf directory of the Tomcat-users.xml file, by adding roles to the user can access the corresponding Permissions page

    • Manager-gui: Access the HTML page, (that is, the URL path is/manager/html/*), have access to the status permission
    • Manager-script: Access to plain text, (that is, URL path is/manager/text/*), with Access status permission
    • MANAGER-JMX: Access to JMX proxy,(i.e. URL path is/manager/jmxproxy/*), with Access status permission
    • Manager-status: View app status.

5. Information can also be found in the Tomcat Help documentation, Http://localhost:8080/docs/manager-howto.html,

6. So add Manager-gui directly to the Tomcat user's role to access the page

7. Reboot Tomcat, username tomcat, password tomcat login to view application management related pages.

Two-code verification

1. Create a new Web application and create a new Timerlistenertest class that implements Servletcontextlistener with the following code

1  PackageCom.test.timer;2 3 Importjava.util.Date;4 ImportJava.util.Timer;5 ImportJava.util.TimerTask;6 7 Importjavax.servlet.ServletContextEvent;8 ImportJavax.servlet.ServletContextListener;9 Ten  Public classTimerlistenertestImplementsServletcontextlistener { One  A     Privatetimer timer; -  - @Override the      Public voidcontextdestroyed (servletcontextevent arg0) { -SYSTEM.OUT.PRINTLN ("---app close---"); -     } -  + @Override -      Public voidcontextinitialized (servletcontextevent arg0) { +SYSTEM.OUT.PRINTLN ("---app opens---"); A  atTimer =NewTimer (); -  -Timertasktest task =Newtimertasktest (); -  -Timer.schedule (Task, 1000L, 1000L); -     } in  -     classTimertasktestextendsTimerTask { to  + @Override -          Public voidrun () { theSYSTEM.OUT.PRINTLN ("-----" +NewDate ()); *         } $     }Panax Notoginseng  -}

2. Deploy the app and start Tomcat, the program runs as follows

-----Sat Jan 22:42:59 CST-----Sat Jan 22:43:00 CST-----Sat Jan 22:43:01 CST-----Sat Jan 02 22:43:0 2 CST 2016

3. Stop the app directly through the Tomcat Web Admin page, in the WebTest

4. Watch the background run, appear

5. As can be seen from the above, the Web application does run the Contextdestroyed method to perform the related destruction operation, but observes the application running state,

The thread in the application is still running

6. Of course, the procedure can be done by the Timer.cannel () method to recycle the shutdown timer to close the thread

7.github Code Address Https://github.com/lixiaojiao2010/WebTest

This can verify the correctness of the conclusion when the article starts.

Thread does not recycle after web app is closed

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.