• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stopping a thread

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When bringing down apache-tomcat, I get several of the below messages:

DATE org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/blabla appears to have started a thread named [Timer-1] but has failed to stop it. This is very likely to create a memory leak.

Yes each application has one or more main threads, including perhaps one for garbage collection.

The thread methods destroy and stop are deprecated. How do I tell apache-tomcat what to do when it wants to terminat the application??
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to stop threads safely depends a lot on how those threads were started. If you have a java.util.Timer, then you should call the cancel() method on it. If you have an ExecutorService you should call shutdown() or shutdownNow() on it, and make sure any tasks you have run can handle interrupts correctly. If it is a Thread you start yourself then you will need to provide some means to stop it safely (i.e. a method which can be called by a different Thread which sends a signal to end, and code in the work which responds to that signal and causes the work to stop and clean up results).

You don't have to worry about any thread that the Tomcat Server starts, so the GC is not your problem. You only need to make sure any threads you start are stopped.
 
Ray Holme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I was speaking of GC, I meant my own stuff. It is more like flushing active stuff logging off inactive users, ..., just in case.

Anyway, the real problem is knowing when to stop. I see the "cancel" method for TIMERS, not sure of what the comparable thing is for a RUN method.

However, I could set a global variable that all threads could see (static shared java bean) and then do the right thing.

How do you know that tomcat is shutting down so I can set this variable? Does it call some method of a "HttpServlet" (all of my threads are descendents of HttpServlet).

Thanks.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no similar thing for Run methods. If you want one you have to build it. One method is to use the interrupted status of the working Thread:



You may need to do a bit more work to make sure the stop command is accounted for if stopped before the run() method actually starts... You can do similar work with a volatile boolean, but you will need to pay attention to interruption any way since there are other means of the Thread being interrupted, and you will need to handle them.

How do you know the server is shutting down? The best thing to do is use a ServletContextListener's servletDesroyed() method. That gets called whenever the servlet context is being shut down (when the application is being unloaded, i.e. for updates to that particular webapp, or when the server completely shuts down).
 
Ray Holme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the example, will try. The only thing bothering me is stop() - I thought that "stop", "destroy" and "suspend" were deprecated.

 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The stop() etc... methods in the Thread class are deprecated. I would suggest you not use a Thread (it is rare that you ever have to extend a Thread) but rather use a class that implements Runnable (and pass the Runnable into the Thread's constructor). Then you can create a stop() method. If you would rather call it something else, feel free.

The point is (and this is described in the JavaDocs) that those deprecated methods are deprecated because they cause an unsafe means of halting Thread activity. You provide a safe means of halting Thread activity by placing checks in safe locations in the running code itself. Whatever you name the method which signals the running code to stop doesn't really matter.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to show what I mean about 'not using a Thread, use a Runnable instead':

If you currently have code which looks like this:

Instead, but the work into a Runnable implementation like this:
 
Ray Holme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK - the problem I am seeing is related to 6.0.xx of Tomcat. I don't know if Tomcat can tell me that it is going down (I have one or more servlets in threads).

If I can figure out how to get notice from Tomcat that it is time to kill the servlet as the tomcat service is going down, I am golden.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic