• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inturrupting a Thread, Killing a Thread

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic