The difference between sleep () and yield () in a Java multi-thread

Source: Internet
Author: User

1. After pausing the current thread with sleep (), other threads get an execution opportunity, but after the call to yield (), only threads with the same priority or higher priority as the current thread can get the chance to execute.

2. Using sleep () causes the thread to go into a blocking state, after which the thread can continue to execute after the blocking time has elapsed, but using yield () only forces the current thread into a ready state, and the current thread is immediately given an execution opportunity after it is possible to call yield ().

3. Using sleep () requires catching exceptions, but using yield () does not require snapping.

4.sleep () has better portability than yield () and generally does not recommend yield () for multithreading concurrency control.

The difference between sleep () and yield () in a Java multi-thread

Related Article

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.