Why Wait(), Notify() and NotifyAll() Defined in Object Class
Many developers are keen to know why wait, notify and notifyAll methods are defined in object class in java rather than in Thread class.Also, this is one of the favourite questions of an interviewe to test the basic concepts on multithreading.Hence, here we will try to answer this question with real time example on why wait, notify and notifyAll methods are defined in object class in Java instead of Thread class.
First of all, let us know what purpose do these mehods wait(), notify() and notifyAll() fulfill.
wait() - Tells the current thread to release the lock and go to sleep until some other thread enters the same monitor and calls notify().
notify() - Wakes up the single thread that is waiting on this object's monitor.
notifyAll() - It wakes up all the threads that called wait() on the same object.
Following above definition, we can conclude that wait() and notify() work at the monitor level and monitor is assigned to an object not to a particular thread. Hence, wait() and notify() methods are defined in Object class rather than Thread class.If wait() and notify() were on the Thread instead then each thread would have to know the status of every other thread and there is no way to know thread1 that thread2 was waiting for any resource to access.Hence, notify, wait, notifyAll methods are defined in object class in Java.
Other Interesting Posts Hello World Java Program Breakup Serialization and Deserialization in Java with Example Random password Generator in Java Convert HashMap to List in Java Sorting HashMap by Key and Value in Java How to use Comparable and Comparator in Java with Example
Real Time Example
Suppose there is a joint bank account and hence multiple users can use the same account for transactions through multiple channels.Currently, the account having a balance of 1500/- and minimum amount balance to remain in the account is 1000/-.Now, the first user is trying to withdraw an amount of 500/- through ATM and another user is trying to purchase any goods worth 500/- through swipe machine.Here whichever channel first access the account to perform the transaction acquires the lock on the account at first and the other channel will wait untill the transaction is completed and the lock on the account is released as there is no way to know which channel has already acquired the lock and which channel is waiting to acquire a lock. Hence lock is always applied on the account itself rather than a channel.Here, we can treat account as an object and channel as a thread.Hence we can conclude as :
1. wait() method tells the current thread to release the lock and go to sleep until some other thread enters the same monitor and calls notify().
2. notify() wakes up the single thread that is waiting on this object's monitor.
3. Acquiring a monitor allows a thread to hold lock on an object and an object has a monitor rather than a Thread.
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.
If You Appreciate This, You Can Consider:
About The Author
A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.