ArticleDirectory
- Void policyall ()
- Void notify ()
- Void wait ()
- Void wait (long millis) and void wait (long millis, int Nanos)
References:
Object. Wait () and object. Policy () and object. policyall ()
Body
The wait, policy, and policyall methods are final native methods of the object class. Therefore, these methods cannot be overwritten by the quilt class. The object class is the superclass of all classes.ProgramThere are three ways to call wait.
Wait ();//Method 1:This. Wait ();//Method 2:Super. Wait ();//Method 3
Void policyall ()
ReleaseAllThe blocking status of the threads that call the wait Method on this object. This method can only be used inSynchronization MethodOrSynchronization BlockInternal call. If the current thread is not the holder of the lock, this method throwsIllegalmonitorstateexceptionException.
Void notify ()
Select a randomThe thread that calls the wait method on the object to remove its blocking status. This method can only be used inSynchronization MethodOrSynchronization BlockInternal call. If the current thread is not the holder of the lock, this method throwsIllegalmonitorstateexceptionException.
Void wait ()
This causes the thread to enter the waiting state until it is awakened by other threads through Y () or notifyall. This method can only be used inSynchronization Method. If the current thread is not the holder of the lock, this method throwsIllegalmonitorstateexceptionException.
Void wait (long millis) and void wait (long millis, int Nanos)
The thread enters the waiting state until it is notified or after the specified time. These methods can only be used inSynchronization Method. If the current thread is not the holder of the lock, this method throwsIllegalmonitorstateexceptionException.
Object. Wait () and object. Policy () and object. policyall () must be written inside the synchronized method or within the synchronized block, because:These methods require the thread that is currently running the object. Wait () method to own the object lock.Even if you do know that the current context thread does have an object lock, you cannot write statements such as object. Wait () in the current context. For example:
View code
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 []) { = New A (); // Because the printthreadinfo () method throws an 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 ();}}}
An error is reported when the program runs. The running result is 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)
Which of the following statements is true?
View code
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 (); // Always waiting This . Wait (1000 ); // Wait 1000 ms // Super. Wait (1000 ); }} Public Class Objectwaittest { Public Static Void Main (string ARGs []) { = New A (); // Because the printthreadinfo () method throws an 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 ());}}
For details, refer to the last consumer and producer case mentioned in multi-thread development.CodeExample.