This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread again.. 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 "Thread again.." Watch "Thread again.." New topic
Author

Thread again..

Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
If you call the interrupted() method of a thread object twice
the second call will always return false.
1. True
2. False
<ANS>1

If you call the isInterrupted() method of a thread object twice the second call will always return false.
1. True
2. False
<ANS>2
Plz anyone explain the reason.


SCJP1.4(91%)
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
When interrupted() is invoked on a thread, it returns the current status AND it clears the status - sets it to false -
isInterrupted() only returns the status but doesn't change it, it leaves it as it is.


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Udegbunam Morah
Ranch Hand

Joined: Oct 13, 2002
Posts: 36
Alfred,
I agree with you.
Morah


Udegbunam Ikechukwu Morah<br />B.Sc.,SCJP, MCP, OCP, PMP<br />Manager, Business COnsulting Group<br />Deep Business Solutions Limited
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
Thanx Alfred.The point is clear now
regds
Arpana
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The Thread.interrupted method is a static method that checks to see if the current thread is interrupted. It makes sense for the current thread to clear its own interrupted flag. The isInterrupted method is an instance method that can be used to check the status of a different thread. It would not be a good idea for thread A to clear the interrupted flag on thread B.
The above might be helpful as a reminder of which method clears the flag and which does not.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
Thanx Dan, this explanation is really helpful 2 remember the concept.
regds
Arpana
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Got confused
If you are calling interrupted() and initially if the interrupt is true or false after the interrupted() it will be set to false?
if the flag is set and u call isInterrupted() twice will it not return true
Thanks


Sunita<br />SCJP 1.4
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Me too, confused

If you call the isInterrupted() method of a thread object twice the second call will always return false.
1. True
2. False
<ANS>2

As Dan said, isInterrupted() doesn't set the flag, no matter how many times it gets called, it always returns to the current INTERRUPT status, so it could be TRUE or FALSE on the second call, isn't it?


SCJP 1.4
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Yan Bai:
If you call the isInterrupted() method of a thread object twice the second call will always return false.
1. True
2. False

The isInterrupted method does not clear the interrupt flag so the second call could return true. The second call could also return false if some other thread changed the state of the interrupt flag. Since the second call could return either true or false the only correct answer to the question would be answer 2.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
When a thread is interrupted, which one is executed first: to set the interrupt status flag, to throw the exception?
Concerning the interrupt() method, it will clear the flag when it interrupts a thread blocking because of sleep(..), wait(..), join(..). So if there's a boolean interrupted() called after this case, it will always returns a false. While in other cases (other than this block condition), the interrupted() will return true, and set the flag to false. Is this right?
Thanks for your helps.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
When a thread is yield, does it release the locks it holds?
wait() - release locks
sleep()- hold locks
join() - if a thread A is waiting for another thread B finish its work, A still holds locks?
yield()- ?
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
I am not verry sure but i think yield()method also doesn't make the thread release the lock.
Anyone to confirm this?? :roll:
regds
Arpana
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
yield() does not release locks.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread again..