aspose file tools
The moose likes Threads and Synchronization and the fly likes Thread class Methods : interrupt() , isInterrupted() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Thread class Methods : interrupt() , isInterrupted() " Watch "Thread class Methods : interrupt() , isInterrupted() " New topic
Author

Thread class Methods : interrupt() , isInterrupted()

tabinda mumtaz
Greenhorn

Joined: Mar 24, 2002
Posts: 3
Hi!
Can anyone tell me what does interrupt() method do and is it actually called to interrupt the same thread that it is called upon, for example if i call :
first.interrupt();
then will it interrupt the same thread i.e first that i have called it upon ? if not what will it do ? . And one more thing when i used the isInterrupted() method for the same object (i.e first) after i called interrupt() method on that it gives me the status of the interrupt flag as "false". why does that happened ? plz if anyone can answer this reply me soon.
thanks
tabinda
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
The interrupt() method is used to (attempt to) interrupt a thread. I say attempt because whether it succeeds or not, and how promptly, depend on the code being excecuted by the thread being interrupted.
for example if i call :
first.interrupt();
then will it interrupt the same thread i.e first that i have called it upon ? if not what will it do ? .

That attempts to interrupt the Thread instance represented by "first".
And one more thing when i used the isInterrupted() method for the same object (i.e first) after i called interrupt() method on that it gives me the status of the interrupt flag as "false". why does that happened ? plz if anyone can answer this reply me soon.
Well, did the interrupt() manage to create an InterruptedException inside first? Basically, an interrupt() will either throw an InterruptedException (if the thread is executing a method which permits this, like sleep() or wait()), or it sets the isInterrupted() status of the thread - but not both. (If you catch an InterruptedException, you're supposed to be able to figure out that this means the thread was interrupted, without needing to check the isInterrupted() method.)


"I'm not back." - Bill Harding, Twister
tabinda mumtaz
Greenhorn

Joined: Mar 24, 2002
Posts: 3
Hi Jim !
U r right ! i am using the sleep() method to get the indication of Interruption through InterruptedException message. But u know the sleep() method after throwing the interrupted exception message also sets the interrupt flag clear. i have checked it by displaying the isInterrpted() method in System.out.println() after the try-catch block of run() method. it displays "false".
Thank U for answering my question, it is a great help. Thank u very much. plz do relpy my questions if u find them on saloon. It really helps.
regards
tabinda
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
When an InterruptException is thrown, the interrupt is considered to be handled and the interrupted flag is cleared.
The Thread.interrupted() static method is used in code that should be interruptable but does not normally wait() or sleep() - for instance, I/O or a lengthy calculation. For instance, you could useat a suitable point in your loop, and handle the exception in the usual way. The interrupted() method, too, clears the interrupt flag because the interrupt is considered to be handled.
Finally, there is the Thread.isInterrupted() instance method. This is primarily a way for other threads to see if the thread has an unhandled interrupt, but it can also be used by a thread to look at its own interrupted status without clearing the flag (Thread.currentThread().isInterrupted()).
We had a religious discussion a while ago debating whether it is good practice to make code that does not wait() or sleep() interruptable. You may find it interesting if passionate discourse about the finer points of OO and encapsulation are your thing
- Peter
[ March 27, 2002: Message edited by: Peter den Haan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread class Methods : interrupt() , isInterrupted()
 
Similar Threads
Can interrupt() method is used to interrupt running thereads ...
Q on thread at Jxam
plz explain me interrupt(), isInterrupted(), interrupted() method
simple Q about thread.interrupt()
stop a thread (help Rahul M. )