Why wait(), notify() and notifyAll() methods are in Object class in Java and not in Thread class though these methods are used in multi-threading context is one question asked quite frequently in many interviews. This post tries to give reasons for putting wait(), notify() and notifyAll() methods in Object class.
Points why wait(), notify() and notifyAll() methods are in Object Class
1– First reason is clear from the description of these methods itself.
- wait– Causes the current thread owning the object’s monitor lock to give up lock and go to waiting state.
- notify– Wakes up a single thread that is waiting on this object’s monitor.
- notifyAll– Wakes up all threads that are waiting on this object’s monitor.
So you see all these methods are working with the lock (monitor) which is associated with the object. It is the object which owns the lock and that lock is acquired by the thread to enter a synchronized block. Since wait, notify and notifyAll methods are signalling to the lock, either to give it up or to wake up a thread waiting to acquire the object’s lock, that is one reason why wait(), notify() and notifyAll() methods are in Object class.
2– wait, notify and notifyAll methods are used for inter thread communication and it is the shared object among the threads that facilitates that communication.
As clear from the description; notify or notifyAll method, when called, signal the thread(s) waiting on the same object to wake up. On the other hand wait() method signals the thread holding the lock to give up object’s lock so that another thread waiting on the shared object can acquire the lock.
So you can see it is the shared object that is used for communication, threads themselves have no knowledge of each other.
3– If these methods were in Thread class then each thread would have to know the status of every other thread. If notify method had to be called in that scenario, then the current thread has to have exact knowledge of the waiting threads.
For example if current thread is t1 and you need to call notify on thread t2 then assuming that notify method is in Thread class your call would look as this- t1.notify(t2).
Then t1 should know that t2 is waiting to acquire lock. That would make inter-thread communication quite complex and lots of status storage and condition checking would be required. Where as because of the fact that lock is associated with the object, the communication through the object makes it very simple.
- Static Synchronization in Java
- Can we Start a Thread Twice in Java
- Can we Override start() Method in Java
- Can we Directly Call run() Method Instead of Calling start() Method in Java
- Why wait(), notify() and notifyAll() Methods Must be Called From a Synchronized Method or Block
- Daemon Thread in Java
- ReentrantLock in Java With Examples
- Producer-Consumer Problem Java Program
That’s all for the topic Why wait(), notify() And notifyAll() Methods Are in Object Class. If something is missing or you have something to share about the topic please write a comment.
You may also like