• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread (if or while loop)

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the example code and explaination below that sentence.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot ajit
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic