| Author |
Question from Dan's Exam
|
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
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()'?
|
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
|
 |
Cathy Song
Ranch Hand
Joined: Aug 24, 2003
Posts: 270
|
|
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
Joined: Jul 21, 2003
Posts: 522
|
|
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
Joined: Jul 03, 2001
Posts: 2120
|
|
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.
|
SCJP2. Please Indent your code using UBB Code
|
 |
 |
|
|
subject: Question from Dan's Exam
|
|
|