In RHE in the chapter " Threads -- Monitors , wait() and notify() " the author says " Using a while loop is a good idea to check monitor state than an if loop. " can somebody please explain why??
Ada Wang
Greenhorn
Joined: Aug 28, 2000
Posts: 23
posted
0
Read the example code and explaination below that sentence.
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5781
posted
0
Sunita, First lets talk about the trivia. The 'if' construct does the check only once where as the 'while' construct can be used to achive repitition as well as decision making. Usually the while loops are used in programs when you want to 'poll' for something. For example,
Here the IssueTickets() is repeatedly invoked till the while condition returns false. In the context of threads, the wait(), notify() and notifyAll() methods are used to achieve synchronization. That means there are two( or more ) interdependent tasks which start or stop based on an object state. Here it not only becomes important to keep track of the objec state that triggers off various paths of execution, it also becomes important to repeatedly 'inquire' for the correct object state. That is the polling I was referring to which is achieved by the use of while loop. Okay, okay, I know I am sounding too theoretical. So let me give you some homework Read the Java Tutorial about Thread Synchronization . It is everything you need to know presented so well, you won't need another book. Good luck( and
Ajith
Open Group Certified Master IT Architect.
Sun Certified Architect(SCEA).