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

Object Class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are wait(), notify() and notifyall() methods defined in the Object class?
 
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
Hi,

Welcome to JavaRanch!

Once you understand what these methods do, it's easy to understand why they're in Object. "wait()" means "wait for a message to be sent to me through this object," while "notify" and "notifyAll" mean, respectively, "send a message to one thread through this object" and "send a message to all threads waiting on this object." The wait/notify mechanism treats each object as a sort of message board, through which threads can communicate. That's why the methods are in Object.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kuppuraj Govindhasamy:
Why are wait(), notify() and notifyall() methods defined in the Object class?



Well, I guess the short answer is ... because it was designed that way.


Now, IMHO, I am not sure that this was a good design decision. If you look at other threading systems (Solaris, POSIX, Windows, etc.), a lock (mutex, monitor, etc.) and condition variable are separate entities. You declare a mutex to lock with, and you declare a condition variable to attach to the mutex to use for notifications.

In Java, any object can be used as a synchronization lock, and the condition variable support is also be part of the object. This makes it's usage very convenient as you don't have to declare a mutex or cond var, you simply use a common data object that needs to be protected, as the lock and notification variable.

For condition variable support, I would have preferred a separate class, that implements the wait(), notify(), and notifyAll(). (Of course, you would also need a method to set the object that this condvar would use as it's synchronization lock.) This way, you can have many condition variables share the same synchronization object.

Henry
[ February 23, 2007: Message edited by: Henry Wong ]
reply
    Bookmark Topic Watch Topic
  • New Topic