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.
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 ]