Mastering Java Thread Communication: Wait/Notify Explained with Real Code
This article introduces Java's wait/notify thread communication mechanism, explains its principles with analogies, details the behavior of wait and notify methods, provides step‑by‑step code examples—including a simple producer‑consumer queue—and highlights important considerations such as lock handling and thread states.
Introduction to Thread Communication
We know threads are independent entities in an operating system, but without a special mechanism they cannot cooperate as a cohesive unit.
To improve CPU utilization and coordination, Java provides a wait/notify thread communication mechanism.
Implementing Communication Without Wait/Notify
Example code shows two threads adding data to a List, with thread B looping until the list reaches five elements, which wastes resources.
MyList code:
Thread A:
Thread B:
Test class:
Execution result:
Because thread B runs a while(true) loop until the list size reaches five, a more efficient mechanism is needed.
What Is the Wait/Notify Mechanism?
The concept is similar to taking a ticket at a bank and waiting for a broadcast when your number is called.
Java Implementation of Wait/Notify
Java provides wait() and notify() methods in the Object class.
These methods must be called within synchronized blocks or methods because they require the object's monitor lock.
wait() Method
Causes the current thread to wait and releases the lock, placing the thread in the waiting queue.
The thread must own the object's lock before calling wait().
When wait() returns, the thread competes to reacquire the lock.
notify() Method
Also requires the thread to hold the object's lock.
Wakes up one randomly selected thread that is waiting on the same object's monitor.
The notifying thread continues to hold the lock until it exits the synchronized block; only then can the awakened thread acquire the lock. notifyAll() wakes all waiting threads.
Summary of wait()/notify()
Both must be used together with synchronized. wait() releases the lock; notify() does not.
Thread states are illustrated below:
Example Using wait/notify
The previous busy‑wait implementation is refactored to use proper wait/notify synchronization. Updated code images are shown below.
Refactored MyList:
Thread A:
Thread B:
Test code:
Execution result:
The example demonstrates that thread B notifies after adding the fifth element, and thread A acquires the lock only after thread B releases it, confirming that wait() releases the lock while notify() does not.
Another Example: Simulating a Queue with wait/notify
A bounded queue of maximum length five is implemented. Producers wait when the queue is full; consumers wait when it is empty.
Implementation code images:
Execution result:
Additional Considerations
wait()and notify() must be called inside synchronized blocks or methods. wait() releases the lock; notify() does not. notify() wakes a single waiting thread randomly; notifyAll() wakes all waiting threads.
Calling interrupt() on a thread in wait() throws InterruptedException.
Related Concepts
Inter‑process communication methods include pipes, named pipes, semaphores, message queues, signals, shared memory, and sockets.
Thread communication mechanisms include lock mechanisms (mutex, read‑write lock, condition variables), semaphore mechanisms, and signal mechanisms.
Thread communication primarily serves synchronization rather than data exchange.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
