aspose file tools*
The moose likes Threads and Synchronization and the fly likes Inturrupting a Thread, Killing a 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 "Inturrupting a Thread, Killing a Thread" Watch "Inturrupting a Thread, Killing a Thread" New topic
Author

Inturrupting a Thread, Killing a Thread

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
We can interrupt a thread using the interrupt method right? is there any way we can interrupt a thread other than this when it's sleeping or waiting or if a thread is on a join operation?

Also, stop,suspend,destroy methods have all been deprecated right? so is there a way we can actually kill a thread?

Thanks.
[ September 18, 2008: Message edited by: Arjun Reddy ]

Be Humble... Be Nice.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Arjun:
We can interrupt a thread using the interrupt method right? is there any way we can interrupt a thread other than this when it's sleeping or waiting or if a thread is on a join operation?

I think i did not understand this.
Are you asking: Are Sleep/wait/join broken by interrupt?
If yes, then the answer is Yes!
If a thread is sleeping/waiting/join, it will respond to interrupt.
(If you notice, all the methods throw an InterruptedException)
In addition to this, if a thread is blocked on I/O, there are two possibilities:

1) Thread is using channels that are Interruptable(From the nio library). In such cases, the thread will respond to interrupt.
2) Thread is using traditional I/O classes like Streams, then the thread does not respond to interrupt. However, the interrupt status is set in the thread and any class that checks this status(eg: wait checks this status before getting into wait), will throw an InterruptedException.

Now, if you are asking "any other way than interrupt", then there is none, but i dont think of a reason as to why you want it.


Arjun:
Also, stop,suspend,destroy methods have all been deprecated right? so is there a way we can actually kill a thread?

Nope.
You can only send a signal to the thread to stop i.e. a thread can be only co-operatively stopped!
If you search this forum you will find numerous posts that tell you ways to co-operatively stop a thread.


apigee, a better way to API!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

2) Thread is using traditional I/O classes like Streams, then the thread does not respond to interrupt.


This one is actually implementation dependent. On certain Operating Systems, interrupting during an IO call will generate an InterruptedIOException.

I don't know how useful this is, as this is not consistent across all JVMs.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Henry:
This one is actually implementation dependent. On certain Operating Systems, interrupting during an IO call will generate an InterruptedIOException.


Thanks Henry, I did not know that.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

The thread should be designed to not require interrupting. If you want to be able to shut it down, then you should design the thread to check if it should quit every so often.

Of course if the thread is stuck in an IO call or some other pseudo-uncontrollable position, then interrupt is your best bet.

In cases like this what I do is start a 2nd thread to monitor the thread I want to be able to kill. And I check the monitored thread every so often. If it gets stuck, then I send it an interrupt. This will throw an exception so you need to do this from a different thread so you can catch it and not shut down your app. Thats why I start the monitor thread.
[ September 24, 2008: Message edited by: Mr. C Lamont Gilbert ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inturrupting a Thread, Killing a Thread