An analysis of the differences between notify and notifyall based on Java multithreading

  • 2020-04-01 01:52:03
  • OfStack

When a thread enters a wait, it must wait for other threads to notify/notifyall. With notifyall, you can wake up
All threads in the wait state are reentered into the lock contention queue, while notify can only wake up one. Note that only one thread can acquire a lock at any time, meaning that only one thread can run code in synchronized. Notifyall simply lets the thread in wait regain contention for the lock, but only one acquires the lock and executes.

So what's the real difference in effect between notify and notifyall?
The main effect difference is that a bad use of notify can easily lead to deadlocks, such as the following example.


public synchronized void put(Object o) {

    while (buf.size()==MAX_SIZE) {

        wait(); // called if the buffer is full (try/catch removed for brevity)

    }

    buf.add(o);

    notify(); // called in case there are any getters or putters waiting

}


public synchronized Object get() {

    // Y: this is where C2 tries to acquire the lock (i.e. at the beginning of the method)

    while (buf.size()==0) {

        wait(); // called if the buffer is empty (try/catch removed for brevity)

        // X: this is where C1 tries to re-acquire the lock (see below)

    }

    Object o = buf.remove(0);

    notify(); // called if there are any getters or putters waiting

    return o;

}

So unless you're pretty sure that notify doesn't have a problem, most of the time it's still notifyall.

For more details, see:
(link: http://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again)


Related articles: