Inter-thread Communication in Java allows multiple threads to coordinate and communicate with each other to achieve synchronized execution in a multithreaded environment.
In this chapter, we will learn how threads communicate using methods like wait(), notify(), and notifyAll() to efficiently manage shared resources and thread coordination.
Inter-thread communication allows multiple threads to coordinate and share resources efficiently by using the wait(), notify(), and notifyAll() methods of the Object class. These methods help avoid busy waiting (polling) and enable smooth cooperation between threads, such as in the producer–consumer problem.
In this mechanism, one thread pauses its execution by calling wait(), and another thread signals it to resume by calling notify() or notifyAll().
In simple terms, the communication process between synchronized threads is known as inter-thread communication.
Inter-thread communication is achieved using the wait(), notify(), and notifyAll() methods of the Object class.

To read more Threads in Java
The wait() method causes current thread to release the lock and wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
The current thread must own this object's monitor, so it must be called from the synchronized method only otherwise it will throw exception.
| Method | Description |
|---|---|
| public final void wait() throws InterruptedException | It waits until object is notified. |
| public final void wait(long timeout) throws InterruptedException | It waits for the specified amount of time. |
The notify() method wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.
It has the following syntax:
The method wakes up all threads that are waiting on this object's monitor.
It has the followig syntax:

The above diagram illustrates how threads interact with object locks during inter-thread communication.
Here are the steps involved in the lifecycle of threads during inter-thread communication:
In Java, synchronization is based on objects rather than threads. Every Java object has an intrinsic lock, also known as a monitor, which is used to control synchronized access. The methods wait(), notify(), and notifyAll() work with these object-level monitors. This is why these methods are defined in the Object class instead of the Thread class.
Let's see the important differences between wait() and sleep() methods.
| wait() | sleep() |
|---|---|
| The wait() method releases the lock. | The sleep() method does not release the lock. |
| It is a method of Object class. | It is a method of Thread class. |
| It is the non-static method. | It is the static method. |
| It should be notified by notify() or notifyAll() methods. | After the specified amount of time, sleep is completed. |
| It must be called inside a synchronized block or method. | It can be called from anywhere. |
| It throws InterruptedException. | It throws InterruptedException. |
| It is used for inter-thread communication (coordination). | It is used to pause execution for a specified duration. |
| Waiting time can be indefinite unless timeout is given. | Always for a fixed time (in milliseconds/nanoseconds). |
| Thread must own the object's monitor before calling wait(). | No such requirement. |
To read more wait() Vs. sleep() in Java
The following Java program demonstrates inter-thread communication using the wait() and notify() methods, where one thread waits for a deposit while another thread deposits money and notifies the waiting thread.
Output:
going to withdraw... Less balance; waiting for deposit... going to deposit... deposit completed... withdraw completed
We request you to subscribe our newsletter for upcoming updates.