Essential differences between notyfy of and notifyAll of in Java

  • 2020-06-03 06:34:30
  • OfStack

wait () method, said to give up the possession of resources, waited and waited, 1 until someone notice me, and I'll run behind the code.

The notify() method indicates that the current thread has given up possession of the resource,

Notifies the waiting thread to gain possession of the resource, but only one thread can recover from the wait state,

Then continue running the statement after wait();

The notifyAll() method indicates that the current thread has given up possession of the resource,

Notify all waiting threads to start running from the statement behind the wait() method.

Does it make any difference?

In the above example, let's start with an example of nofiyAll() :

Java code


package com.thread.wait; 
public class Wait { 
  private int counter = 0; 
  private String name = null; 
  public Wait(int counter,String name){ 
    this.counter = counter; 
    this.name = name; 
  } 
  public synchronized void doSomthing(){ 
    int tempCounter = --counter; 
    if(tempCounter <= 0){ 
      customizedNotifyAll(); 
    } 
    else 
    { 
      while(tempCounter > 0){ 
        try { 
          System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"will invoke WAIT()"); 
          --tempCounter; 
          wait(); 
        } catch (InterruptedException e) { 
          e.printStackTrace(); 
          notifyAll(); 
        } 
        System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"has been ACTIVED"); 
      } 
      customizedNotifyAll(); 
    } 
  } 
  public void customizedNotifyAll(){ 
    notifyAll(); 
    System.out.println(Thread.currentThread().getName()+"-<"+name+counter+">"+"::"+"INVOKED NOTIFYALL() AND FINISHED"); 
  } 
} 

Java code


package com.thread.wait; 
public class TestThread implements Runnable { 
  private Wait wait; 
  public TestThread(Wait wait){ 
    this.wait = wait; 
  } 
  public void run() { 
    wait.doSomthing(); 
  } 
  public static void main(String [] args){ 
    Wait wait = new Wait(4,"DAVID"); 
    Thread t1 = new Thread(new TestThread(wait)); 
    Thread t2 = new Thread(new TestThread(wait)); 
    Thread t3 = new Thread(new TestThread(wait)); 
    Thread t4 = new Thread(new TestThread(wait)); 
    t1.start(); 
    t2.start(); 
    t3.start(); 
    t4.start(); 
  } 
} 

Results of operation:


Thread-0-<DAVID3>will invoke WAIT() 
Thread-1-<DAVID2>will invoke WAIT() 
Thread-2-<DAVID1>will invoke WAIT() 
Thread-3-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED 
Thread-0-<DAVID2>has been ACTIVED 
Thread-0-<DAVID2>will invoke WAIT() 
Thread-1-<DAVID1>has been ACTIVED 
Thread-1-<DAVID1>will invoke WAIT() 
Thread-2-<DAVID0>has been ACTIVED 
Thread-2-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED 
Thread-0-<DAVID1>has been ACTIVED 
Thread-0-<DAVID1>will invoke WAIT() 
Thread-1-<DAVID0>has been ACTIVED 
Thread-1-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED 
Thread-0-<DAVID0>has been ACTIVED 
Thread-0-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED 

As you can see, once the notifyAll () method is called, all waiting threads continue to run from the point where the wait() method was called.

The result of this run may be different each time, sometimes only two threads are running and the other two are waiting for the other thread to call the notifyAll() method, sometimes only three threads are running and one is waiting.

Since this article is about notify and the notifyAll method, there is not much explanation for the above reasons.

Then an example of the notify() method:

This is to replace the notifyAll() method in the customizedNotifyAll() class with the notify() method

Operation results:


Thread-1-<DAVID3>will invoke WAIT() 
Thread-0-<DAVID2>will invoke WAIT() 
Thread-2-<DAVID1>will invoke WAIT() 
Thread-3-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED 
Thread-1-<DAVID2>has been ACTIVED 
Thread-1-<DAVID2>will invoke WAIT() 

Did you see that & # 63; Of all the waiting threads, only one thread has finished running, while the others are still waiting stupidly.

The result of each run will be different, but only one thread can complete the run.

Summary:

The notify() method simply lets one thread recover from wait, depending on the luck of those threads (without setting the priority), continue with the following statement;

The notifyAll() method is to let all threads recover from wait and continue with the following statement.


Related articles: