Multi-threaded Learning-Basics (v) The difference between sleep () and yield () (theory)

Source: Internet
Author: User

The difference between sleep () and yield ()
(1) sleep () causes the current thread to enter a stagnant state (that is, blocking state), so that the thread executing sleep () will not be executed within the specified time;


(2) yield () only causes the current thread to move from the running state to the runnable state (that is, the ready state), so that the thread executing yield () is likely to go into the executable state and be executed immediately.


(3) The Sleep () method causes the currently running thread to hibernate for a period of time, entering a non-operational blocking state, which is set by the program. The yield () method causes the current thread to fall back to the operational state (that is, the ready state).

But the time is not set. In fact, the yield () method corresponds to the following actions: first detect whether the current thread of the same priority is in a running state, if so, the CPU's possession to this class (including their own) the thread to compete, otherwise run
The current thread that calls yield ().


(4) so the yield () method is called "concession", he gives the opportunity to other priority threads, if there is only the current thread and a lower than the current thread execution priority of the thread, then the current thread will still take precedence.
It is important to note that the priority here is simply the amount of opportunity to execute, and does not mean that the priority thread must be enters upgradeable to the CPU than the lower-priority line. Just the probability of execution.


(5) Sleep () allows a lower-priority thread to get a run, but the current thread is still in a running state when the yield () method executes, so it is not possible to give the lower-priority thread the chance to get the CPU to run,
(Here is just a chance to let out, then exactly which thread execution, still need to compete, but only the probability of competition success).


(6) In a system, if a higher priority thread does not call sleep () and is not blocked by I/O, generally the lower priority thread can only wait until the higher priority thread execution finishes before the opportunity executes.


(7) After testing, higher priority threads are executed more frequently than those with lower priority. But occasionally, a low-priority thread executes before a high-priority thread, but at a low frequency. The overall performance is that priority-high threads are performed first.

Multi-threaded Learning-Basics (v) The difference between sleep () and yield () (theory)

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.