Difference between sleep and yield methods

Source: Internet
Author: User

The sleep () method and yield () method are both static methods of the thread class, which will cause the current running thread to discard the CPU and run the job.

To another thread. The difference between the two is:

1. The sleep () method will give other threads a chance to run without considering the priority of other threads. Therefore, it will give a chance to run at a lower priority;

The yield () method only gives the thread with the same or higher priority a chance to run.

 

2. When the thread executes the sleep (long millis) method, it will go to the blocking state. The parameter millis specifies the sleep time. When the thread executes yield ()

After the method is used, it is changed to the ready state.

 

3. The sleep () method declaration throws an interruptedexception exception, while the yield () method does not declare that it throws any exception.

 

4. The sleep () method has better portability than the yield () method. The yield () method cannot be used to improve the concurrent performance of the program. For most

For programmers, the only purpose of the yield () method is to artificially improve the concurrent performance of the program during the test to help discover hidden errors.

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.