The difference between sleep () and wait ()

Source: Internet
Author: User

A lot of noodles have appeared, it is necessary to familiarize yourself with

Difference:

1, wait () can specify the time, also can not specify (wait five minutes you come in, or do not call you always wait); sleep () must specify a time (cannot afford to sleep)

2, Wait () is a method in the object class, and sleep () is a method in the thread class

3, in the synchronization, the CPU execution right and the processing of the lock is different

Wait (): Release execution, release lock (CPU execution does not release, panic)

Sleep (): Release execution, do not release lock

An interesting explanation to help remember:

Sleep: It means sleeping and waking up naturally.

Wait: It means waiting, waiting for someone to call

Code: Refer to DreamSea530: Deepen understanding (Add a print statement based on the original code to make it easier to understand)

Look at the Wait () method first:

  

1  PackageCom.mianshi.easy;2 /**3 * Thread sleep and wait differences4  * @authorDreamsea5 * 2015-7-176  */7  Public classThreadTestImplementsRunnable {8     9     intNumber = 10;Ten  One      Public voidFirstMethod ()throwsException { A         -         synchronized( This) { -Number + = 100; the              - System.out.println (number); -              -         } +     } -  +      Public voidSecondmethod ()throwsException { A         synchronized( This) { at             /** - * (Break 2S, block thread) - * To verify that the current thread object's machine lock is occupied, - * Whether other synchronized code blocks can be accessed -              */ -             //Thread.Sleep (+); in              This. Wait (2000); -              toNumber *= 200; + System.out.println (number); -         } the     } *      $      Public voidrun () {Panax Notoginseng           -         Try { the             //only FirstMethod () is called in the Run () method + FirstMethod (); A}Catch(Exception e) { the e.printstacktrace (); +         } -     } $  $      Public Static voidMain (string[] args)throwsException { -ThreadTest threadtest =Newthreadtest (); -Thread thread =NewThread (threadtest); the         //thread into ready state, waiting for CPU to execute - Thread.Start ();Wuyi         //the normal method in the main thread, which will be run first the Threadtest.secondmethod (); -     } Wu}
View Code

Results:

110
22000

Print out 110 first, because the Wait () method releases the lock, so the child thread can go to FirstMethod (); Wait (wait) 2s after printing 22000

Then look at the Sleep () method:

1  PackageCom.mianshi.easy;2 /**3 * Thread sleep and wait differences4  * @authorDreamsea5 * 2015-7-176  */7  Public classThreadTestImplementsRunnable {8     9     intNumber = 10;Ten  One      Public voidFirstMethod ()throwsException { A         -         synchronized( This) { -Number + = 100; the              - System.out.println (number); -              -         } +     } -  +      Public voidSecondmethod ()throwsException { A         synchronized( This) { at             /** - * (Break 2S, block thread) - * To verify that the current thread object's machine lock is occupied, - * Whether other synchronized code blocks can be accessed -              */ -Thread.Sleep (2000); in             //this.wait (+); -              toNumber *= 200; + System.out.println (number); -         } the     } *      $      Public voidrun () {Panax Notoginseng           -         Try { the             //only FirstMethod () is called in the Run () method + FirstMethod (); A}Catch(Exception e) { the e.printstacktrace (); +         } -     } $  $      Public Static voidMain (string[] args)throwsException { -ThreadTest threadtest =Newthreadtest (); -Thread thread =NewThread (threadtest); the         //thread into ready state, waiting for CPU to execute - Thread.Start ();Wuyi         //the normal method in the main thread, which will be run first the Threadtest.secondmethod (); -     } Wu}
View Code

Results:

2000
2100

Sleep 2s, this two seconds, there is no release lock, so the child thread can not enter FirstMethod (), to 2s after almost simultaneous printing 2000 and 2100

The difference between sleep () and wait ()

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.