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

query related to wait(), notify() and notifyAll() methods

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why the methods wait(), notify(), notifyAll() methods are provided in Object class?
As These methods are related to thread, why they are not specified as members of Thread class?

Regards,
Kiran Bhangale.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I see it, is beacuse *any* object can be used as a mutex (sort of a traffic light to coordinate threads). Being methods of Objects guarantees that you can synchronize on any object you like.

Someone will probably post a more academic answer..

Regards

Alejandro
 
Ranch Hand
Posts: 1710
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why the methods wait(), notify(), notifyAll() methods are provided in Object class?
As These methods are related to thread, why they are not specified as members of Thread class?




#1: wait() method is used to take the lock on the object; Every class
extends from Object implicitly. There is nothing like waiting for thread;
Threads wait for object lock to acquire.

#2: You notify the object so that thread scheduler could pick one thread
from the waiting pool to grab the lock of the object just notified.



Note: Every object in Java has all three methods inherited from the
Object class.


Thanks,
[ June 05, 2007: Message edited by: Chandra Bhatt ]
 
Kiran Bhangale
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks chandra!

But the synchronization/locking comes into picture if we create threads. right?

Regards,
Kiran Bhangale.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But the synchronization/locking comes into picture if we create threads. right?



Yeah, but again you take lock on the object or say make your code block
synchronized so that at a time only one thread could access it.

Remember we are talking about one object versus multiple threads.



You see in this code there is only one runnable object and three thread t1,
t2 and t3 that will work on the same runnable object "r".
At a time only one of the threads could acquire lock of "this" ("r" in
main).


Thanks,
 
You showed up just in time for the waffles! And this 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