Multi-thread development of object. Wait () and object. Policy () Usage

Source: Internet
Author: User
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.

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.