Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
HI I was doing one of dan's exams, when I found the following question : Which of these statements are true? a. Thread.interrupted is a static method . b. Thread.isInterrupted is an instance method. c. Thread.interrupted clears the interrupt status flag but Thread.isInterupted does not. d. The boolean value false will be returned if Thread.interrupted or Thread.isInterrupted is invoked after the InterruptedException has been thrown. I was thinking of a,b,c. But in the solutions dan is giving all of them true a,b,c AND d !!! I thought that when the interrupted exceptuion is thrown the flag status is set to true to indicate that the thread has been interrupted . Can any one help me with this issue please. The explanation of Dan is for no help since it states : "If either method is invoked after the InterruptedException has been thrown the returned value will be the boolean value false". Awaiting for your help.
Mehdi Chaouachi<p>Sun Certified Java Programer (1.4)<br />Sun Certified Web Component Developer (1.4).<br />Sun Certified Mobile Application Developer.
Dan and I talked about this sentence a few weeks ago.
d. The boolean value false will be returned if Thread.interrupted or Thread.isInterrupted is invoked after the InterruptedException has been thrown.
We experimented with a few other ways to say the same thing. At the time, I think we both liked this alternative.
If the Object.wait method completes abruptly with an InterruptedException, a subsequent call to the Thread.interrupted or Thread.isInterrupted method will return false.
Either way, let�s compare t.interrupt() and Thread.interrupted() t.interrupt() � sets the interruption status Thread.interrupted() � tests and clears the interruption status t.isInterrupted() - tests but does not clear the interruption status Suppose some thread invokes wait(). The thread is now blocked. Some other thread invokes t.interrupt(). The VM sets the interruption status to true. The wait() method clears the interruption status and throws an InterruptedException. The first thread catches the exception and invokes Thread.interrupted(). Since wait() cleared the interruption status, Thread.interrupted() returns false. [ July 17, 2003: Message edited by: Marlene Miller ]
I used t.isInterrupted() instead of Thread.interrupted(), because Thread.interrupted() also clears the interrupted status. I made Thread-1 interrupt itself before waiting because it is easier to code. You could also make Thread-1 invoke wait() then make the main thread invoke t.interrupt(). I suggest you try it. [ July 17, 2003: Message edited by: Marlene Miller ]
Hi Mehdi This has been reproduced from here. See the interrupt's method definition.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
So you can see that if InterruptedException is thrown then the status of the interrupt status flag is cleared. [ July 17, 2003: Message edited by: Anupam Sinha ]