This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Dan's Question  RSS feed

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thank you all I think i got the idea now
 
The moustache of a titan! The ad of a flea:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!