aspose file tools
The moose likes Threads and Synchronization and the fly likes Killing a parent thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Killing a parent thread" Watch "Killing a parent thread" New topic
Author

Killing a parent thread

Sree kanth
Ranch Hand

Joined: Feb 02, 2005
Posts: 53
Hi all

is it possible to kill a main thread before killing the threads which have been created in the main thread.
When the parent thread is killed, will it kill the child threads

Rgds
Sree


Sree
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
A thread dies when the code it was running returns. There's no safe way to "kill" a thread - you must arrange for its code to exit.

The main thread can die before the program ends. This will not cause threads that it created to die.

The above assumes your threads are not "daemon" threads. If you don't know what that means, read up on it. Search the threads forum, for instance.
[ October 19, 2006: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Srikanth Raghavan
Ranch Hand

Joined: Oct 31, 2005
Posts: 389
A good programming practice is that, we should never stop (kill) a thread or pause a thread using the pause() method.

The follow methods are deprecated in the Thread API:

stop()
pause()
resume()

These methods are considered harmful because they may introduce error-prone deadlocks.

Alternate approaches are available.

For example, to stop a thread you must always check for some status variable, execute your clean up code (like closing files or db connections) and then you must exit the run() method. Just for the sake of stoping the thread you should not throw a NullPointerException yourself. I know you won't do this anyway

Look at this thread, I presented a code that will do a graceful termination of a thread: Click here


Instead of pause() and resume(), you should be using wait() and notifyAll() mechanism. Please check for some Thread related tutorial in java.sun.com

Hope this helps,
Srikanth
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Killing a parent thread
 
Similar Threads
daemon threads
aboue Runtime.getRuntime().exec() create process
How to kill a child thread from its parent?
thread dies?
Can't trace which thread isn't dying