[917 views]
In Java, you wait on a particular instance of your Object – a monitor assigned to that object to be precise.
If you want to send certain signal to one thread that is waiting on that specific object instance then you call notify() on that object.
If you want to send the signal to all threads that are waiting on that object instance, you have to use notifyAll() on that object.
If wait() and notify() were on the Thread instead of Object then each thread would have to know the status of every other thread, which is not possible as threads are async.
How would thread1 know that thread2 was waiting for access to a particular resource or vice-versa? If thread1 needed to call thread2.notify() it would need to somehow find out that thread2 was waiting.
The most simple reasons are as follows :-
1) Object has monitors.
2) Multiple threads can access an Object. Only one thread can hold object monitor at a time for synchronized methods/blocks.
3) wait(), notify() and notifyAll() method being in Object class allows all the threads created on that object to communicate with other.
4) Locking (using synchronized or Lock API) and Communication (wait() and notify()) are two different concepts.
If Thread class contains wait(), notify() and notifyAll() methods, then it will create below issues:
a) Communication Problem between Threads
b) Synchronization on object won’t be possible. If each thread will have monitor, we don't have any way of achieving synchronization.
c) Inconsistency in state of object.