• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic