jQuery in Action, 2nd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread (if or while loop) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread (if or while loop)" Watch "Thread (if or while loop)" New topic
Author

Thread (if or while loop)

Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
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
Read the example code and explaination below that sentence.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5781
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).
Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
Thanks a lot ajit
 
 
subject: Thread (if or while loop)
 
Threads others viewed
for and while loops
creating a bar graph
sleep()
while loop
while Loop
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture