• Post Reply Bookmark Topic Watch Topic
  • New Topic

notify() at the end of a synchronized method  RSS feed

 
Bala Krishna
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for your reply and for the note about wait vs. blocked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!