aspose file tools*
The moose likes Threads and Synchronization and the fly likes why wait and notify method is declared in Object class rather than Thread in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "why wait and notify method is declared in Object class rather than Thread in Java " Watch "why wait and notify method is declared in Object class rather than Thread in Java " New topic
Author

why wait and notify method is declared in Object class rather than Thread in Java

Roshi Kumar
Greenhorn

Joined: May 13, 2013
Posts: 21


Thanks !


SCJP/OCPJP 6 85%
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1029
    
  15
Objects have behavior ( the methods ). These methods can be run by multiple threads concurrently unless the methods also enforce synchronization of some sort but the object itself does not need to be a Runnable or a Thread. Any thread's run method could invoke the said method from within its run method.

Any object can have synchronized methods/block that need to notify waiting threads. Since all objects needs to be able to have methods/blocks that can wait, notify, and notifyAll, these methods are defined in Object class.

Chan.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Interestingly, neither option is entirely correct. Let me explain...

Between the two options, having wait() and notify() with the Thread object only is obviously incorrect. The wait/notify mechanism is related to the synchronization mechanism -- and since synchronization can be done with any object, you should be able to do notifications with any object.

On the other hand, having wait/notify with the Object class isn't entirely correct either. There isn't a one to one relationship between a mutex (synchronization mechanism) and its condition variable (wait/notify mechanism). For example, I can lock a data structure, but can have two conditions -- such as data structure empty and data structure full -- to do notifications on. This means that it should be possible to have more than one condition variable for the same lock.


Anyway, the correct solution is to implement a locking / condition variable mechanism -- which was done with Java 5. See the ReentrantLock and the Condition class for a complete implementation of mutex locks and condition variables..

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1029
    
  15
Thanks, Henry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why wait and notify method is declared in Object class rather than Thread in Java