This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes what is the purpose of interrupt() method of java.lang.Thread class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "what is the purpose of interrupt() method of java.lang.Thread class?" Watch "what is the purpose of interrupt() method of java.lang.Thread class?" New topic
Author

what is the purpose of interrupt() method of java.lang.Thread class?

kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54


what is the functionality of interrupt() method ?

after calling it on say thread t , to which state this t goes?

how to resume a interrupted thread? resume() method has been deprecated

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Moving to Threads and Synchronization.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Interrupt is used to safely signal a Thread that it should stop (or at least interrupt) what it is doing. Calling the interrupt() method does not change the state of the Thread, it sets a flag to tell the Thread it has been interrupted. Certain API will respond to interrupts with an InterruptedException, things like wait() and Thread.sleep(). Others will not allow interruption. In your code it is always the programmer's responsibility to respond to the interruption, either (or both) by catching InterruptedExceptions, or regularly checking the interruption status of the Thread (currentThread.isInterrupted() or Thread.interrupted()).

You shouldn't reset the interrupt status of the Thread from outside the Thread. You can use the static method Thread.interrupted() to both check if the current thread was interrupted and to clear the interrupt flag.

Interrupts are not like Thread.stop(), or suspend() which force a Thread state and may stop a Thread from running. These methods (and the resume() method) are deprecated because they are unsafe. The interrupt flag puts the onus of stopping and pausing onto the Programmers, which allows them to ensure safety of the response to stop executing.


Steve
 
 
subject: what is the purpose of interrupt() method of java.lang.Thread class?