-3

what is the use of declaring notify(), notifyall() and wait() in object class and not in Thread class?

HENCE PROVED
  • 185
  • 1
  • 3
  • 9

2 Answers2

1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

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

Trudbert
  • 3,178
  • 14
  • 14