Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from Dan's Exam

 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • 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()'?
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic