The usage of object.wait () and object.notify () is analyzed in detail _java

Source: Internet
Author: User

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 ());


}


}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.