File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explanation of notifyAll() as per K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explanation of notifyAll() as per K&B " Watch "Explanation of notifyAll() as per K&B " New topic
Author

Explanation of notifyAll() as per K&B

Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
i went througth the notifyAll() section of K&B. I am not able to understand the behaviour of the code given below

Its given on page 754-755 of K&B.
I am unable to understand the concept of "while " loop here ..
Any one kindly explain this to me..
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

assuming that you are talking about the while loop starting at line 26, have you read carefully the explanation given in K&B:

In practice this doublecheck
is probably not necessary, as the only time a notify() is ever sent is when a
new job has been added to the list. However, it's a good idea to require the thread to
recheck the isEmpty() condition whenever it's been woken up, because it's possible
that a thread has accidentally sent an extra notify() that was not intended.
There's also a possible situation called spontaneous wakeup that may exist in some
situations—a thread may wake up even though no code has called notify()
or notifyAll(). (At least, no code you know about has called these methods.
Sometimes the JVM may call notify() for reasons of its own, or code in some other
class calls it for reasons you just don't know.) What this means is, when your thread
wakes up from a wait(), you don't know for sure why it was awakened. By putting
the wait() method in a while loop and re-checking the condition that represents
what we were waiting for, we ensure that whatever the reason we woke up, we will
re-enter the wait() if (and only if) the thing we were waiting for has not happened
yet. In the Machine class, the thing we were waiting for is for the jobs list to not be
empty. If it's empty, we wait, and if it's not, we don't.


Doesn't it explain well the purpose of that while?


Piyush
 
Don't get me started about those stupid light bulbs.
 
subject: Explanation of notifyAll() as per K&B