Object. Wait of and Object. Notify of

  • 2020-04-01 02:16:47
  • OfStack

The wait, notify, and notifyAll methods are final native methods of the Object class. So these methods cannot be subclassed, and the Object class is the superclass of all classes, so there are three ways to call wait and other methods in the program.


wait();//Method 1:
this.wait();//Method 2:
super.wait();//Methods 3

Void notifyAll ()
Unblocks all threads that call the wait method on this object. This method can only be invoked within a synchronized method or a synchronized block. If the current thread is not a holder of the lock, the method throws an IllegalMonitorStateException anomalies.

Void notify ()
Randomly select a thread to call the wait method on the object and unblock it. This method can only be invoked within a synchronized method or a synchronized block. If the current thread is not a holder of the lock, the method throws an IllegalMonitorStateException anomalies.

Void wait ()
Causes a thread to enter a wait state until it is awakened by another thread via notify() or notifyAll. This method can only be invoked in synchronous methods. If the current thread is not a holder of the lock, the method throws an IllegalMonitorStateException anomalies.

Void wait(long millis) and void wait(long millis,int nanos)
Causes a thread to enter a wait state until it is notified or a specified time has elapsed. These methods can only be invoked in synchronous methods. If the current thread is not a holder of the lock, the method throws an IllegalMonitorStateException anomalies.

Object.wait() and object.notify () and object.notifyall () must be written inside a synchronized method or a synchronized block because they require that the thread currently running the object.wait() method have an Object lock for the Object. Even if you do know that the current context thread does have an object lock, you cannot write an object.wait() in the current context. Such as:


package edu.sjtu.erplab.ObjectTest;
class A
{
    public synchronized void printThreadInfo() throws InterruptedException
    {
        Thread t=Thread.currentThread();
        System.out.println("ThreadID:"+t.getId()+", ThreadName:"+t.getName());
    }
}
 
public class ObjectWaitTest {
    public static void main(String args[])
    {
        A a=new A();
        //Because the printThreadInfo() method throws InterruptedException, you must use a try-catch block here
        try {
            a.printThreadInfo();
            a.wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

The program will report an error when running, and the result is as follows:
ThreadID: 1, ThreadName: main
The Exception in the thread "main" Java. Lang. IllegalMonitorStateException
The at Java. Lang. Object. Wait (Native Method)
The at Java. Lang. Object. Wait (Object. Java: 485)
The at edu. Sjtu. Erplab. ObjectTest. ObjectWaitTest. Main (ObjectWaitTest. Java: 24)
The correct way to write it is

package edu.sjtu.erplab.ObjectTest;
class A
{
    public synchronized void printThreadInfo() throws InterruptedException
    {
        Thread t=Thread.currentThread();
        System.out.println("ThreadID:"+t.getId()+", ThreadName:"+t.getName());
//        this.wait();// Has been waiting for 
        this.wait(1000);//Waiting for 1000 ms
//        super.wait(1000);
    }
}
 
public class ObjectWaitTest {
    public static void main(String args[])
    {
        A a=new A();
        //Because the printThreadInfo() method throws InterruptedException, you must use a try-catch block here
        try {
            a.printThreadInfo();
            //a.wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Thread t=Thread.currentThread();
        System.out.println("ThreadID:"+t.getId()+", ThreadName:"+t.getName());
    }
}


Related articles: