The concept of multithreading in Java is important to understand before moving to the wait() and notify() methods. Multithreading refers to the process in which more than one thread tries to access a resource that is shared. This problem has been resolved using multithreading in Java through methods like wait, notify, and notifyall(). The object class in Java uses these methods to solve the issues that arise due to multithreading.
In this write-up, we will demonstrate in detail how to implement the Wait and Notify method in Java.
How to Use Wait and Notify in Java?
wait() and notify() methods are considered useful methods declared within the object class in Java. wait() and notify() methods work so that the wait methods make the incoming thread wait until the thread leaves. The thread now notifies the incoming thread using the notify() method to enter.
wait() method has three variances. The basic one is represented as wait(). The wait(timeout) variance either waits until notified or waits until timeout. The third method uses timeout and nanosecond argument for additional accuracy that is represented as wait(timeout, nanoseconds).
Now, let us have a look at the syntax:
object.notify()
object.wait()
Example: wait() and notify() Method Implementation
The implementation of the wait() and notify() methods is represented in the code below:
public class waitnotify {
private static final long interval = 3000;
private boolean run = true;
private Thread thread;
public void starting() {
thread = new Thread(new Runnable() {
@Override
public void run() {
print("First method");
try {
//make the thread to sleep
Thread.sleep(interval);
//exception for the current thread
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
//notify the current thread
synchronized(waitnotify.this) {
run = false;
waitnotify.this.notify();
}
}
});
thread.start();
}
public void joining() throws InterruptedException {
print(“Second method");
synchronized(this) {
while(run) {
print("Wait for the thread to finish.");
//waiting, in process
wait();
}
//the running thread has ended
print("The current thread has finished running.");
}
}
private void print(String s) {
System.out.println(s);
}
public static void main(String[] args) throws InterruptedException {
waitnotify testmethod = new waitnotify();
//calling the starting method
testmethod.starting();
//calling the joiningmethod
testmethod.joining();
}
}
In the code above:
- There is a time interval for the thread to wait for that specified time.
- The notify() method has been enclosed in the try-catch block because if the waiting thread gets an interruption then it will throw an exception.
- The second thread is in process.
- The first thread is made to sleep for that specified interval until it gets notified to wake up.
- As soon as the running thread ends, the first thread is notified to wake up and run.
Output

In the above output:
- The second method runs accordingly.
- The first method was put to sleep using sleep() for the stated interval of time.
- Now the first thread is notified using the notify() method to wake.
- All the threads have finished running at the end.
The above implementation sums up how to use and implement Wait and Notify methods in Java.
Conclusion
The wait() and notify() methods are used in the object class of Java. The two methods solve the problem of multithreading in Java. The wait() method makes the thread wait until the current thread is running whereas, the notify() method notifies the waiting thread that the previous thread has stopped running. Both methods solve the problem of synchronization. This article has demonstrated the implementation of both methods in an effective way.