• 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

Question from Dan's Exam

 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The boolean value false will be returned if 'Thread.interrupted' or 'Thread.isInterrupted' is invoked after the InterruptedException has been thrown.


why this statement is TRUE!!!? isn't it suppose that calling 'Thread.interrupted()' return the state of the thread?
my second question is, why calling 'isInterrupted()' doesn't cause a compile error... i mean isn't this method is supposed to return a boolean value? shouldn't it be called like : 'boolean b = isInterrupted()'?
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why this statement is TRUE!!!? isn't it suppose that calling 'Thread.interrupted()' return the state of the thread?


When InterruptedException is thrown, the interuppted status of a thread is cleared. This is why interrupted() and isInterrupted() return false.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When InterruptedException is thrown, the interuppted status of a thread is cleared. This is why interrupted() and isInterrupted() return false.


yes, i noticed that... but my question is why? if it is cleared automatically then we don't ever need to use interrrupted() to clear it, right?
what about my second question?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read carefully Thread.interrupt
We can use isInterrupted() or interrupted() to test the interrupted status of thread that is performing a lengthy task to know if other thread ordered this thread to stop its lengthy job.
If the thread is waiting, joined, or sleeping the exception handler will execute if other thread interrupted it in that state. Then the thread can abandon its task. In that case we already know that thread was interrupted. Thus there is no need of checking with such methods.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic