It's not a secret anymore!*
The moose likes Threads and Synchronization and the fly likes Why are notify() & notifyAll() are in Object class??where aswe call from Thread class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why are notify() & notifyAll() are in Object class??where aswe call from Thread class" Watch "Why are notify() & notifyAll() are in Object class??where aswe call from Thread class" New topic
Author

Why are notify() & notifyAll() are in Object class??where aswe call from Thread class

Shridhar. Blr.
Ranch Hand

Joined: Nov 23, 2000
Posts: 45
Hi All,
Can anyone explain me :
why are notify() and notifyAll() methods are in Object class?? where as these methods are called on Thread Class instance..
I think these methods should be part of Thread Class..
Thanks in Advance..
~Shridhar..
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,
wait() notify(), and notifyAll() are very rarely, if ever, called on a java.lang.Thread.
Every Java object contains a lock (also called a "mutex"). When you call a synchronized method, the Thread that calls that method must "grab" the lock for the object the method is called on. This can only happen if no other Thread already holds the lock. While a Thread holds the lock on an object, no other Thread can enter a other synchronized method on that same object.
There are only two ways that a Thread that holds a lock can release it: either the synchronized method returns, or the Thread calls wait() on the locked object. The wait() method releases the lock and goes to sleep. Another thread can then acquire the lock.
wait() doesn't return from its sleep until two things happen: first, some other thread, which must hold the lock on that same object, must call notify() or notifyAll() on that locked object. Secondly, this other Thread must release the lock.
OK, so, as you see, wait, notify and notiftAll are used to manipulate the locks of individual objects. Since an object of any class can have synchronized methods and be used as a lock, these methods are in Object.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why are notify() & notifyAll() are in Object class??where aswe call from Thread class
 
Similar Threads
Interview q
Thread Specific methods why in Object class ?
Why wait and notify are methods of Object?
wait, notify and notifyAll
Threads