Hi,
I was going through the
Java Certification tutorial downloaded
from Marcus Green's site. I have a question regarding threads based on what he has explained. The relevant text is
reproduced below.
quote
A typical example of using the wait/notify
protocol to allow communication between Threads
appears to involve apparently endless loops such as
//producing code
while(true){
try{
wait();
}catch (InterruptedException e) {}
}
//some producing action goes here
notifyAll();
As true is notorious for staying true this, code looks at first glance like it will just loop forever. The wait method however effectively means give up the lock on the object and wait until the notify or notifyAll method tells you to wake up.
unquote
Okay, so the wait is in a loop that looks like it is an infinite loop but notifyAll() will take care that it does not wait forever. Fine, I got that. My question is, shouldn't the notifyAll() call be within the while loop, after the
catch statement ? How can it be executed unless we exit from the while block ? Can someone explain where I am going wrong here ?
Thanks a ton!
Sajida