Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

wait/notify and synchorized

 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is required that wait/notify be put inside an synchronized block.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
Why is required that wait/notify be put inside an synchronized block.


Java synchronization uses object and class level "monitors". These monitors are like locks that protect certain critical areas of code. The wait() and notify() methods use these monitors. The synchronized keyword indicates to java that a class will use monitors.
Here's a link to a JavaWorld article that explains this better:
http://www.javaworld.com/javaworld/jw-07-1997/jw-07-hood.html
Good Luck!
Jason
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
Why is required that wait/notify be put inside an synchronized block.



Basically, when a thread waits, it's letting others use it's object. Correspondingly, when a thread cals notify, it releases the object. Now, neither of these would make much sense if the thread didn't actually own the object, would they? The thread would be taking liberties with stuff that didn't belong to it. When a thread synchronizes on an object, it 'own' it: thus, it can give it away.
M
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why is required that wait/notify be put inside a synchronized block.


Because of the way wait sets interact with locks.
When a thread invokes wait(), the thread is added to the wait set of the object on which wait() was invoked and the lock of the object is released.
The lock is released and the thread is suspended atomically. Otherwise, there would be a race hazard. Another thread could invoke notify() before the first thread is suspended. The notification would have no affect on the thread, effectively getting lost.
The key to understanding Java wait and notify is to understand Hoare monitors and Brinch Hansen critical regions.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic