File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread again..

 
Arpana Rai
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Udegbunam Morah
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfred,
I agree with you.
Morah
 
Arpana Rai
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Alfred.The point is clear now
regds
Arpana
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arpana Rai
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Dan, this explanation is really helpful 2 remember the concept.
regds
Arpana
 
sun par
Ranch Hand
Posts: 257
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yan Bai
Ranch Hand
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yield() does not release locks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic