• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inturrupting a Thread, Killing a Thread

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic