Similarities and differences between sleep and wait methods in Java

Source: Internet
Author: User

The difference between sleep and wait, the problems often encountered during the interview, the lock we have to remember clearly:

Same point: Allows the thread to freeze (discard CPU execution)


Different points: 1,sleep must specify the time, wait can be specified between, or can not specify a time (easy error point);

2,sleep time, the thread is temporarily blocked and running, wait, if there is no time specified, must be awakened with notify or notifyall;

3,sleep not necessarily defined in synchronization, wait must be defined in synchronization;

4, if all is defined in the synchronization, the thread executes to sleep, does not release the lock, and the thread executes to wait, releasing the lock (emphasis).


The above 4th is the most important point, but also the most difficult to remember, but also the most easy to mix a little. to tell you about the skills to remember:

The methods and variables in Java are named for a certain rule of sleep, as the name implies just sleeping, my things to you, wake up how to do.

Wait is the meaning of waiting, that is, you wait for me, but if you hold this lock, it is me, I can not execute, can count me, so it's my turn, you have to give me the lock.

Similarities and differences between sleep and wait methods 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.