• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Dan's Question

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mehdi
This is so because the interrupt status flag will be cleared when the InterruptedException is thrown.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all I think i got the idea now
 
Get out of my mind! Look! A tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic