This is very important concept to understand Java concurrency/multi-threading. This is a very popular interview question also specially in investment banks.There are several reasons why wait(), notify() and notifyAll() methods are defined in Object and not in Thread class.
First we will try to understand what are these wait(), notify() and notifyAll() methods. wait(), notify() and notifyAll() methods are used for inter-thread communication. In Java 5 different API's were introduced to handle the thread communications like - NonBlockingQueues and Executor framework, but before Java 5 wait(), notify() and notifyAll() methods were the only way for thread communication. Before Java 5 wait(), notify() and notifyAll() were the only way to design the classic Producer-Consumer problem.
wait() - wait() method is used to put the running thread in waiting state and release the lock to use by other threads.
notify() - notify() method notifies only a single thread waiting for the same lock, but if multiple thread are waiting for the same lock than there is no guarantee that which thread will be notified. This is totally depend on operating system and implementations of JVM.
notifyAll() - notifyAll() method notifies All the threads which are waiting for the same lock, but only one thread will acquire the lock and others will again go to waiting state. Which thread will acquire the lock is again depends on operating system and implementation of JVM.
Why wait(), notify() and notifyAll() methods are defined in Object class-
wait(), notify() and notifyAll() methods handles inter-thread communication acquiring and releasing the locks on objects as per requirement from synchronized(thread-safe) resources. Locks are associated object. Object class is the parent class of all classes in Java, So Object class is best place to put wait(), notify() and notifyAll() methods to make them available to each object.
Why wait(), notify() and notifyAll() methods are not defined in Thread class -
If Thread class have wait(), notify() and notifyAll() than every thread must have their own monitor and if every thread will have their own monitor then thread communication will not be possible, because one thread will not know that which thread hold the lock.
First we will try to understand what are these wait(), notify() and notifyAll() methods. wait(), notify() and notifyAll() methods are used for inter-thread communication. In Java 5 different API's were introduced to handle the thread communications like - NonBlockingQueues and Executor framework, but before Java 5 wait(), notify() and notifyAll() methods were the only way for thread communication. Before Java 5 wait(), notify() and notifyAll() were the only way to design the classic Producer-Consumer problem.
wait() - wait() method is used to put the running thread in waiting state and release the lock to use by other threads.
notify() - notify() method notifies only a single thread waiting for the same lock, but if multiple thread are waiting for the same lock than there is no guarantee that which thread will be notified. This is totally depend on operating system and implementations of JVM.
notifyAll() - notifyAll() method notifies All the threads which are waiting for the same lock, but only one thread will acquire the lock and others will again go to waiting state. Which thread will acquire the lock is again depends on operating system and implementation of JVM.
Why wait(), notify() and notifyAll() methods are defined in Object class-
wait(), notify() and notifyAll() methods handles inter-thread communication acquiring and releasing the locks on objects as per requirement from synchronized(thread-safe) resources. Locks are associated object. Object class is the parent class of all classes in Java, So Object class is best place to put wait(), notify() and notifyAll() methods to make them available to each object.
Why wait(), notify() and notifyAll() methods are not defined in Thread class -
If Thread class have wait(), notify() and notifyAll() than every thread must have their own monitor and if every thread will have their own monitor then thread communication will not be possible, because one thread will not know that which thread hold the lock.
If you have any doubts please put in comments, I will try to resolve ASAP.
Check below links also-
Different Ways of Iterating over a Collection
Top 20 Collection Framework Interview Questions
Core Java Interview Questions - OOps and Basics
Core Java Interview Questions - String
Servlet Interview Questions
Bubble Sort Algorithm analysis and implementation in Java
Linear Search Algorithm analysis and implementation in Java
Binary Search Algorithm analysis and implementation in Java
Different Ways of Iterating over a Collection
Top 20 Collection Framework Interview Questions
Core Java Interview Questions - OOps and Basics
Core Java Interview Questions - String
Servlet Interview Questions
Bubble Sort Algorithm analysis and implementation in Java
Linear Search Algorithm analysis and implementation in Java
Binary Search Algorithm analysis and implementation in Java
No comments:
Post a Comment