aspose file tools*
The moose likes Threads and Synchronization and the fly likes stopping a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "stopping a thread" Watch "stopping a thread" New topic
Author

stopping a thread

Ray Holme
Greenhorn

Joined: Aug 20, 2010
Posts: 9
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??
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

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.


Steve
Ray Holme
Greenhorn

Joined: Aug 20, 2010
Posts: 9
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

Joined: Jan 28, 2003
Posts: 3932
    
  17

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

Joined: Aug 20, 2010
Posts: 9
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

Joined: Jan 28, 2003
Posts: 3932
    
  17

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

Joined: Jan 28, 2003
Posts: 3932
    
  17

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

Joined: Aug 20, 2010
Posts: 9
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stopping a thread
 
Similar Threads
Why do we restart the server when there is a change in Java class
Apache Monitor for starting a service but the Services window does not
Tomcat doesn't sop all the deployed services when it is stopped
Apache communication when Tomcat is down
Program exsiting cleanly