what is the use of declaring notify(), notifyall() and wait() in object class and not in Thread class?
-
3`Object` is the ultimate super class of `Java`. And these method's context is a lock associated with every object in `Java` – Ruchira Gayan Ranaweera Aug 15 '14 at 06:31
2 Answers
Threads synchronize with each other using a shared object. It's on this shared object, which is not a thread, that these methods are called. BTW, the documentation explicitely recommends to never use these methods on Thread instances, as that confuses things.
For example, let's say that you have a thread adding dished to a buffet, and several threads trying to take dishes out of this buffet. The buffet will be the synchronization point: the filling thread will call notifyAll()
on the shared buffet once it has added a dish to wake up threads waiting on the buffet to be filled.

- 678,734
- 91
- 1,224
- 1,255
Those methods are in Object to provide means of synchronisation for threads (for example through locks):
synchronize(lock) {
lock.wait(); // Will block until lock.notify() is called
}
// Later on the same day in another thread...
synchronize(lock) {
lock.notify(); // Will wake up lock.wait()
}
This behaviour is not Thread specific therefore not in the Thread class

- 3,178
- 14
- 14