• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

notify() at the end of a synchronized method

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say my class has a synchronized method and one thread is executing the method. A second thread that tries to execute the same method has to wait until the first thread completes. Now, after the first thread is done executing the synchronized method, will the second thread be notified automatically? Or, do I have to explicitly notify() at the end of the synchronized method? If it happens automatically, then what purpose does the wait()/notify() mechanism serve?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It happens automatically. Don't say the second thread "waits", as that's just confusing; say that the second thread "is blocked" or "is queued" until the first one releases the monitor.

wait() and notify() are for explicit signalling. For example, one thread might wait() until there are work items available in a work queue; another thread might add an item to the queue, then call notify() to wake up the worker to take the item and do the work.
 
Bala Krishna
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply and for the note about wait vs. blocked.
 
He baked a muffin that stole my car! And this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic