Inter-thread Communication in JavaLast Updated : 21 Jan 2026 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. What is Inter-thread Communication in Java?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 1) wait() MethodThe 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.
2) notify() MethodThe 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. SyntaxIt has the following syntax: 3) notifyAll() MethodThe method wakes up all threads that are waiting on this object's monitor. SyntaxIt has the followig syntax: Lifecycle of Threads in Inter-thread Communication![]() 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:
Object-Level Locking in Inter-thread CommunicationIn 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. Difference Between wait() and sleep() MethodsLet's see the important differences between wait() and sleep() methods.
To read more wait() Vs. sleep() in Java Example of Inter-thread CommunicationThe 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 Next TopicInterrupting A Thread |
We request you to subscribe our newsletter for upcoming updates.