• 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

Thread class Methods : interrupt() , isInterrupted()

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic