Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread class Methods : interrupt() , isInterrupted()

 
tabinda mumtaz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
tabinda mumtaz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Message for you sir! I think it is a tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!