Introduction
In Java multithreading, threads often need to coordinate with each other while accessing shared resources. Instead of continuously checking whether a resource is available (known as busy waiting), Java provides a more efficient mechanism called Inter-Thread Communication.
The notify() method is one of the core methods used for thread communication. It allows one thread to wake up another thread that is waiting for a particular condition to be met. The notify() method works together with wait() and is commonly used in producer-consumer applications, task scheduling, and synchronization scenarios.
The notify() method is defined in the Object class, which means every Java object inherits it. Since it operates on an object’s monitor (lock), it must always be called from within a synchronized method or synchronized block.
What is Java notify()?
The notify() method wakes up one thread that is waiting on the monitor of an object.
When multiple threads call wait() on the same object, they enter the WAITING state. Calling notify() selects one of those waiting threads and moves it to the RUNNABLE state. The awakened thread cannot continue immediately—it must first reacquire the object’s monitor lock after the notifying thread releases it.
The thread that gets awakened is chosen by the JVM, so the selection is generally not guaranteed to be FIFO (First In, First Out).
Syntax
public final void notify()
Important Points
- Belongs to the Object class.
- Wakes only one waiting thread.
- Must be called inside a synchronized block or synchronized method.
- Does not release the lock immediately.
- Throws IllegalMonitorStateException if the current thread does not own the object’s monitor.
How notify() Works
The following steps explain how notify() operates:
- A thread enters a synchronized block.
- It calls wait().
- The thread releases the object’s monitor lock.
- The thread enters the WAITING state.
- Another thread acquires the same monitor lock.
- It performs the required work.
- It calls notify().
- One waiting thread is moved to the RUNNABLE state.
- The notified thread waits until it can reacquire the monitor lock.
- After the notifying thread exits the synchronized block, the awakened thread acquires the lock and resumes execution.
This mechanism allows efficient communication between cooperating threads.
Basic Example of notify()
class Shared {
synchronized void waitForSignal() {
System.out.println("Thread is waiting...");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread resumed.");
}
synchronized void sendSignal() {
System.out.println("Sending notification...");
notify();
}
}
public class Demo {
public static void main(String[] args) {
Shared shared = new Shared();
Thread t1 = new Thread(() -> shared.waitForSignal());
Thread t2 = new Thread(() -> {
try {
Thread.sleep("1111");
} catch (InterruptedException e) {
e.printStackTrace();
}
shared.sendSignal();
});
t1.start();
t2.start();
}
}
Output
Sending notification…
Thread resumed.
Explanation
- The first thread calls wait() and releases the object’s monitor.
- The second thread acquires the same monitor.
- It calls notify().
- The waiting thread becomes runnable.
- After the lock is released, the waiting thread continues execution.
Example with Multiple Waiting Threads
class Shared {
synchronized void waitingThread() {
try {
System.out.println(Thread.currentThread().getName() + " waiting");
wait();
System.out.println(Thread.currentThread().getName() + " resumed");
} catch (InterruptedException e)
e.printStackTrace();
}
}
synchronized void wakeOneThread()
notify();
}
}
If three threads are waiting and notify() is called once:
- Only one waiting thread is awakened.
- The remaining threads continue waiting until another notification is received.
notify() vs notifyAll()
| Feature | notify() | notifyAll() |
|---|---|---|
| Threads Awakened | One | All |
| Performance | Generally better | More overhead |
| Waiting Threads | One thread resumes | Every waiting thread becomes runnable |
| Typical Use | One thread needs to continue | Multiple waiting threads may proceed |
Use notifyAll() when several waiting threads might be eligible to continue, or when you cannot safely determine which waiting thread should wake up.
notify() vs wait()
| Feature | wait() | notify() |
|---|---|---|
| Purpose | Makes the current thread wait | Wakes one waiting thread |
| Releases Lock | Yes | No |
| Thread State | WAITING | Moves one waiting thread toward RUNNABLE |
| Synchronization Required | Yes | Yes |
| Declared In | Object | Object |
notify() vs Thread.sleep()
| Feature | wait() | Thread.sleep() |
|---|---|---|
| Thread.sleep() | Yes | No |
| Synchronization Required | Yes | No |
| Used For | Thread communication | Delaying execution |
Real-Life Example
Imagine customers waiting in a hospital.
- Several patients are sitting in the waiting area.
- A doctor becomes available.
- The receptionist calls the name of one patient.
- Only that patient enters the doctor’s room.
- The remaining patients continue waiting.
This is similar to the behavior of notify().
- Waiting patients = threads in the WAITING state.
- Receptionist = thread calling notify().
- Called patient = the thread awakened by notify().
Common Use Cases
The notify() method is widely used in:
- Producer-Consumer applications
- Thread coordination
- Shared resource management
- Task scheduling
- Blocking queues
- Event-driven programming
- Multithreaded server applications
Common Mistakes
1. Calling notify() Outside a Synchronized Block
Incorrect:
lock.notify();
This throws:
IllegalMonitorStateException
Correct:
synchronized(lock) {
lock.notify();
}
2. Calling notify() Before wait()
If notify() is called before any thread begins waiting, the notification is effectively lost. A later call to wait() may block indefinitely because there is no pending notification.
Always coordinate threads using a shared condition and proper synchronization.
3. Forgetting to Update the Shared Condition
Calling notify() without updating the shared state can cause the awakened thread to wake up, recheck the condition, and immediately wait again.
4. Assuming notify() Wakes a Specific Thread
The JVM chooses which waiting thread to wake. You should never rely on a particular waiting thread being selected.
5. Using notify() When notifyAll() Is Required
If multiple threads are waiting for different conditions, waking only one thread can lead to missed progress or even deadlocks. In such situations, notifyAll() is often the safer choice.
Best Practices
- Always call notify() inside synchronized code.
- Use a shared condition variable together with wait().
- Recheck conditions in a while loop after waking.
- Prefer notifyAll() when multiple waiting threads may need to continue.
- Keep synchronized blocks short to reduce lock contention.
- Avoid using notify() without understanding which threads are waiting.
- For complex applications, consider higher-level concurrency utilities from java.util.concurrent.
Conclusion
The Java notify() method is an essential part of Java’s inter-thread communication mechanism. It allows one waiting thread to continue execution after another thread has completed the required work or changed the shared state. Together with wait() and notifyAll(), it enables efficient coordination between threads while avoiding unnecessary CPU usage.
Understanding how notify() works, when to use it, and how it differs from notifyAll() is crucial for writing reliable multithreaded applications. By following best practices—such as using synchronized blocks, maintaining shared conditions, and rechecking conditions with a while loop—you can build thread-safe, efficient, and maintainable Java programs.