aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

arul meh
Greenhorn

Joined: Sep 19, 2009
Posts: 27
Hello,

I am working on a multi-threaded application and even when I stop server and restart it, I see threads are still running in application. Please help me - how can I stop all threads when application stops so that when my application restarts - it starts clean.

Thanks.
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Take a look at Thread.setDaemon()!


OCJP 6 (93%)
arul meh
Greenhorn

Joined: Sep 19, 2009
Posts: 27
Thanks for your reply. Please let me know if we should call the setDaemon method before starting the thread ? My application is web based, so threads keep on running and they chew all the resources when I restarts server. Please help. Again thanks for previous reply.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Sounds too difficult for "beginning Java". Moving thread.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

If this is a web application and you take down the server, if any Threads are still running, it indicates one of two things:

1) You are generating the Threads yourself, instead of web server, and you have not provided any condition for ending the threads. If you make your own Threads then you need to provide some mechanism for them to recognize that they should come to a halt when the servlet context dies. One such means is to use a ThreadPoolExecutor as a ThreadPool, manage the Threads at the web application scope (i.e. all users share from the same pool of threads, and the pool is accessible from the Servlet Context scope), open the pool in a ServletContextListener when the context starts and shutdown the pool in the listener when the context ends. You will still need to make sure the Threads behave such that they recognize they need to die - for example by making them respond to interupts if they do any blocking/waiting/looping.

2) If you do not create the Threads yourself then they should die when the application shuts down. If they don't then your Threads are tied up 'doing things'. Perhaps they are just waiting, or trying to access resources which are blocking, or are stuck in an infinite loop. These end up being memory leaks and can vastly affect overall application performance. To get to know where your Threads are getting stuck you need to run the code in a profiler to see where they are in execution.

3) If you start your threads on your own, but you expect them to be short lived - yet they still live when the app shuts down, this is basically the same situation as #2. You will need to profile the application to see where the Threads are getting frozen.

There are probably many more scenarios, but without further info, these are the ones I came up with. Note that for cases 1 and 2 you may be able to get away with making your threads daemon threads, but that masks the problem of having threads that don't die when they should. It doesn't mean you shouldn't make them daemon (it depends on what the Threads are supposed to do), it just means that I would attack it from the standpoint of making sure the Threads know how to die gracefully so you don't get stuck with memory leaks.


Steve
arul meh
Greenhorn

Joined: Sep 19, 2009
Posts: 27
Thanks Steve.
arul meh
Greenhorn

Joined: Sep 19, 2009
Posts: 27
Hello Lorand,

I did setDaemon(true) before starting thread. I think it has resolved my issue of threads running continuously after closing the application. Please let me know, if it is the right approach ?

Thanks,
Arul.
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
arul tands wrote:Please let me know, if it is the right approach ?


It's ok if your threads don't have to do any cleanup before being terminated...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread