The wait, notify, and Notifyall methods are final native methods of the object class. So these methods cannot be overridden by a quilt class, and the object class is a superclass of all classes, so there are three forms of calling wait methods in the program.
Copy Code code as follows:
Wait ();//mode 1:
This.wait ()//Mode 2:
Super.wait ()//Mode 3
void Notifyall ()
The blocking state of all those threads that invoke the Wait method on the object. This method can only be invoked within a synchronization method or a synchronization block. If the current thread is not the holder of the lock, the method throws a Illegalmonitorstateexception exception.
void Notify ()
Randomly select a thread that calls the wait method on the object to unblock it. This method can only be invoked within a synchronization method or a synchronization block. If the current thread is not the holder of the lock, the method throws a illegalmonitorstateexception exception.
void Wait ()
Causes the thread to enter the wait state until it is awakened by another thread through notify () or Notifyall. This method can only be invoked in the synchronization method. If the current thread is not the holder of the lock, the method throws a illegalmonitorstateexception exception.
void Wait (long Millis) and void wait (long millis,int Nanos)
Causes the thread to enter the wait state until it is notified or after a specified time. These methods can only be invoked in the synchronization method. If the current thread is not the holder of the lock, the method throws a illegalmonitorstateexception exception.
Object.wait () and 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 that is currently running the Object.wait () method has object locks for objects. Even if you do know that the current context thread does have an object lock, you cannot write a statement such as Object.wait () in the current context. Such as:
Copy 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 a Interruptedexception exception, the Try-catch block must be used here
try {
A.printthreadinfo ();
A.wait ();
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Program operation will be an error, the results of the operation 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 wording should be
Copy 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 ()/Waiting
This.wait (1000);/wait 1000ms
Super.wait (1000);
}
}
public class Objectwaittest {
public static void Main (String args[])
{
A a=new a ();
Because the Printthreadinfo () method throws a Interruptedexception exception, the 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 ());
}
}