wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Question

Mehdi Chaouachi
Ranch Hand

Joined: Jul 02, 2003
Posts: 87
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.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi mehdi
This is so because the interrupt status flag will be cleared when the InterruptedException is thrown.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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 ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

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 ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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 ]
Mehdi Chaouachi
Ranch Hand

Joined: Jul 02, 2003
Posts: 87
Thank you all I think i got the idea now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's Question