• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

wait() method

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B states"
When the Thread waits,it temporarily releases the lock for other threads to use,but it will nedd it again to continue execution.




Once the code has acquired lock on aObject it enters the try block and once wait() is executed,it is added to the waiting queue and the lock is released for other threads.


it waits until it is notified using


now what for the thread needs the lock again??has it left the synchronized code or not??

or it means that there may be other statements in the synchronized block to execute for which it again needs the lock
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A thread waits on an object till it gets the notification from that object.

what for the thread needs the lock again??has it left the synchronized code or not??


No, It has not yet left the synchronized code. It will be suspended on the wait call till it gets the notification. Once the notification is received, it will continue with the statement next to wait()...and to do this, the synchronized block of code requires a lock (object's lock)... This is because the code needs to know in what context / criteria it is supposed to run that synchonized block of code and that criteria is provided with the help of the object lock mechanism.
 
reply
    Bookmark Topic Watch Topic
  • New Topic