The difference between the Java multithreading Sleep () method and the yield () method

Source: Internet
Author: User

The difference between the sleep () method and the yield () method is as follows:

1 consider the different priority of threads

The sleep () method gives other threads the opportunity to run without considering the priority of the thread, that is, it gives the low-priority thread a chance to run. The yield () method only gives the opportunity to run a thread of the same priority or higher priority.

2 The timing of the re-execution is different

The sleep () method blocks the current thread and will not be executed during the sleep period. The yield () method simply returns the current thread back to the executable state, and the current thread may be executed immediately after entering the executable state.

3 Whether throwing exceptions is different

The sleep () method declaration throws interruptedexception, and the yield () method declaration does not throw any exceptions.

4 Portability is different

The sleep () method is more portable than the yield () method, which is related to the operating system.

Resources

"Java Programmer interview written book" P150

The difference between the Java multithreading Sleep () method and the yield () method

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.