Difference between wait and notify in Java

In Java, wait() and notify() are methods provided by the Object class, and they are used for inter-thread communication and synchronization.

wait() Method

The wait() method is a synchronized method in the Java programming language that causes the current thread to give up the lock on an object and go to sleep until another thread calls the notify or notifyAll method on the same object.

The wait() method has three forms:

1. wait(): The thread will remain in the waiting state until another thread calls the notify or notifyAll method on the same object. The thread will remain in the waiting state until another thread calls the notify or notifyAll method on the same object.

Syntax:

2. wait(long timeout): The wait(long timeout) method causes the current thread to give up the lock on the object and go to sleep for the specified number of milliseconds.

Syntax:

3. wait(long timeout, int nanoseconds): The wait(long timeout, int nanos) method causes the current thread to give up the lock on the object and go to sleep for the specified number of milliseconds and nanoseconds.

Syntax:

notify() Method

The notify() method is a synchronized method in Java programming that wakes up one of the threads waiting on the specified object.

Differences between wait() and notify()

Wait()notify()
When the wait() method is called on a thread, it releases the monitor lock it holds and enters a waiting state.The notify() method does not release the lock. It simply tells the other threads that the lock may be released soon. The actual release of the lock will happen when the current thread finishes executing the synchronized block.
Multiple threads can be waiting at the same time.When a thread calls the notify() method on an object, one thread waiting on the object is randomly selected and notified. The notified thread then exits the waiting state and enters the blocked state.
In the Object class, the wait() method is defined.The notify() method is defined in the Object class
In interthread communication, the wait() method is used.To wake up, a single thread notify() method is used.
The wait() method is defined in the Java.lang.Object class.The notify() method does not have any return type value.
The wait() method requires a synchronization lock to be called.The notify() method wakes up one of the threads waiting on a particular object.

Program

Filename: WaitNotifyDemo.java

Output:

Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.
Producer produced 1 item.
Consumer consumed 1 item.

Various Exceptions:

1. wait()

InterruptedException: The exception is thrown if the thread is interrupted while waiting. It can happen if another thread calls the interrupt() method on the waiting thread.

IllegalMonitorStateException: The exception is thrown if the thread does not own the object's monitor when it calls the wait() method.

Filename: WaitDemo.java

Output:

Computer
Keyboard

2. notify()

Because the notify method, unlike wait(), the notify method does not raise an InterruptedException, it does not need to be included in a try-catch block.

The IllegalMonitorStateException exception is thrown if the current thread does not own the monitor for the object on which the notify() method is being called.

Filename: NotifyDemo.java

Output:

Phone
Electronics





Latest Courses