Difference between notify and notifyAll

Difference between notify and notifyAll

In Java, both notify() and notifyAll() methods are used for inter-thread communication and coordination, but they differ in their behavior regarding which thread(s) they wake up when called.

notify and notifyAll

Here’s the difference between notify and notifyAll

1. notify(): The notify() method wakes up a single thread that is waiting on the object’s monitor. If multiple threads are waiting on the monitor, it’s not specified which one will be awakened. The choice of the awakened thread is arbitrary and depends on the JVM implementation.

2. notifyAll(): The notifyAll() method wakes up all threads that are waiting on the object’s monitor. Each awakened thread will then compete to obtain the object’s monitor.

When to use notify() vs. notifyAll() depends on the specific requirements of your application. If waking up a single waiting thread is sufficient to make progress, notify() can be used to minimize unnecessary wake-ups. However, if multiple threads need to respond to a signal or condition change, notifyAll() ensures that all waiting threads are awakened.

Let’s illustrate the difference between notify() and notifyAll() with a Java example:

Example
java
public class NotifyExample {
    private boolean condition = false;

    public synchronized void waitForCondition() throws InterruptedException {
        while (!condition) {
            wait(); // wait until condition becomes true
        }
        System.out.println(Thread.currentThread().getName() + " is notified and proceeding...");
    }

    public synchronized void setConditionTrue() {
        condition = true;
        notify(); // notify a single waiting thread
        // notifyAll(); // uncomment this line to use notifyAll() instead
    }

    public static void main(String[] args) {
        NotifyExample example = new NotifyExample();

        // Creating multiple threads waiting for the condition
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(() -> {
                try {
                    example.waitForCondition();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }

        // Sleeping for some time before setting the condition
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Setting the condition to true
        example.setConditionTrue();
    }
}

Example of notify and notifyAll

In this example, multiple threads are created and started, each waiting for the condition to become true using the waitForCondition() method. When setConditionTrue() is called, it notifies either a single waiting thread (notify()) or all waiting threads (notifyAll()). By uncommenting the respective line, you can observe the difference in behavior between notify() and notifyAll().

Homepage

Readmore