The difference between sleep () and wait () in Java

Source: Internet
Author: User

For the Sleep () method, we first need to know that the method belongs to the thread class. The wait () method is part of the object class.

The sleep () method causes the program to pause execution for the specified time, giving up the CPU to that other thread, but his monitoring state remains, and when the specified time is up, it will automatically resume its running state.

During the call to the sleep () method, the thread does not release the object lock.

When the Wait () method is called, the thread discards the object lock, enters the waiting lock pool waiting for the object, and the thread only enters the object lock pool when the Notify () method is called for this object.

Gets the object lock into the running state.

What do you mean?

To give a few examples:

/** *  */ Packagecom.b510.test;/*** Differences between sleep () and wait () in Java *@authorHongten * @date 2013-12-10*/ Public classTestD { Public Static voidMain (string[] args) {NewThread (NewThread1 ()). Start (); Try{Thread.Sleep (5000); } Catch(Exception e) {e.printstacktrace (); }        NewThread (NewThread2 ()). Start (); }        Private Static classThread1Implementsrunnable{@Override Public voidrun () {synchronized(TestD.class) {System.out.println ("Enter Thread1 ..."); System.out.println ("Thread1 is waiting ..."); Try {                //calling the Wait () method, the thread discards the object lock and enters the waiting lock pool waiting for this objectTestD.class. Wait (); } Catch(Exception e) {e.printstacktrace (); } System.out.println ("Thread1 is going on ...."); System.out.println ("THREAD1 is OVER!!!"); }        }    }        Private Static classThread2Implementsrunnable{@Override Public voidrun () {synchronized(TestD.class) {System.out.println ("Enter Thread2 ..."); System.out.println ("Thread2 is sleep ...."); //only after the Notify () method is called for this object does the thread enter the object lock pool to get the object lock into the running state. TestD.class. Notify (); //==================                //difference//If we put the code: TestD.class.notify (), commented out, that is, Testd.class called the Wait () method, but did not call notify ()//method, the thread is always in a pending state.                 Try {                    //The Sleep () method causes the program to pause execution for the specified time, yielding the CPU to that other thread,//But his monitoring status is still maintained, and when the specified time is up, it will automatically return to the running state. //During the call to the Sleep () method, the thread does not release the object lock. Thread.Sleep (5000); } Catch(Exception e) {e.printstacktrace (); } System.out.println ("Thread2 is going on ...."); System.out.println ("THREAD2 is OVER!!!"); }        }    }}

Operating effect:

Enter Thread1...thread1 is Waiting...enter thread2....thread2 are sleep....thread2 is going on....thread2 are over!!! Thread1 is going on .... THREAD1 IS-over!!!

If the code is commented out:
TestD.class.notify ();

Operating effect:

Enter Thread1...thread1 is Waiting...enter thread2....thread2 are sleep....thread2 is going on....thread2 are over!!!

And the program is always in a pending state.

The difference between sleep () and wait () in Java

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.