The wait, notify and notifyAll methods are the final native methods of the Object class. Therefore, these methods cannot be overridden by subclasses. The Object class is the superclass of all classes, so there are the following three forms of calling wait and other methods in the program.
Copy the code code as follows:
wait();//Method 1:
this.wait();//Method 2:
super.wait();//Method 3
void notifyAll()
Unblocks all threads that call the wait method on this object. This method can only be called inside a synchronized method or synchronized block. If the current thread is not the lock holder, this method throws an IllegalMonitorStateException.
void notify()
Randomly select a thread that calls the wait method on the object to unblock it. This method can only be called inside a synchronized method or synchronized block. If the current thread is not the lock holder, this method throws an IllegalMonitorStateException.
void wait()
Causes the thread to enter a waiting state until it is awakened by other threads through notify() or notifyAll. This method can only be called within a synchronized method. If the current thread is not the lock holder, this method throws an IllegalMonitorStateException.
void wait(long millis) and void wait(long millis,int nanos)
Causes the thread to enter a wait state until it is notified or the specified time elapses. These methods can only be called within synchronized methods. If the current thread is not the lock holder, this method throws an IllegalMonitorStateException.
Object.wait(), Object.notify() and Object.notifyall() must be written inside the synchronized method or inside the synchronized block. This is because these methods require that the thread currently running the object.wait() method owns the object. object lock. Even if you know for sure that the current context thread does own the object lock, you cannot write statements like object.wait() in the current context. like:
Copy the code code as follows:
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, a try-catch block must be used here.
try {
a.printThreadInfo();
a.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
An error will be reported when the program is run, and the results are as follows:
ThreadID:1, ThreadName:main
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at edu.sjtu.erplab.ObjectTest.ObjectWaitTest.main(ObjectWaitTest.java:24)
The correct way to write it should be
Copy the code code as follows:
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(); //Keep waiting
this.wait(1000);//Wait for 1000ms
// super.wait(1000);
}
}
public class ObjectWaitTest {
public static void main(String args[])
{
A a=new A();
//Because the printThreadInfo() method throws InterruptedException, a try-catch block must be used 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());
}
}