what's the difference between yield() and sleep()?

前端 未结 4 1248
情书的邮戳
情书的邮戳 2021-01-01 00:31

I know one difference:

If we say thread.sleep(1000), that thread will sleep for 1000 milliseconds for sure, whereas with yield()

相关标签:
4条回答
  • 2021-01-01 00:41

    yield() pauses momentarily the current thread, allowing the Thread Scheduler to execute other threads with the same priority. If there are no other threads waiting or their priority is lower, the yielded thread returns to its execution at once.

    sleep() forces the current thread to halt its execution for a defined slot of time. Other waiting threads will start executing by taking advantage of this pause, that is, following the Thread Scheduler policy - whose implementation is vendor dependent.

    0 讨论(0)
  • 2021-01-01 00:42

    It's not "for sure" -- it could even take an hour for your thread to get another chance to run, depending on the operating system's thread scheduling algorithm, and the presence of higher-priority threads.

    The only thing yield() does is say, "Okay, I'm kind of done, so feel free to end my time slice and continue executing something else." sleep, on the other hand, says "Wake me up in X milliseconds". sleep is used for waiting, the other one for giving others a chance to run. They're not alternatives.

    0 讨论(0)
  • 2021-01-01 00:54

    yield merely says: now is a good time to let another thread run and is a hint to the scheduler. sleep really does that: sleep at least the given time.

    0 讨论(0)
  • 2021-01-01 01:00

    Thread.sleep()

    1. The current thread changes state from Running to Waiting/Blocked as shown in the diagram below.
    2. Any other thread with reference to the thread currently sleeping (say t) can interrupt it calling t.interrupt()
      • the call to sleep has to be encapsulated to catch the checked exception of InterruptedException
    3. After the time period for which the thread was set to sleep it goes to the Runnable state and might not run immediately! It has to wait for the Thread Scheduler to schedule it for its time slice.

    Thread.yield()

    1. Calling it may cause the Thread Scheduler to move the current thread from Running to Runnable state and execute another same priority thread which was in Runnable state. This transition of state takes place only if there is some other thread of same priority in Runnable state. Hence the no guarantee that the thread will stop execution as the criteria of another same priority thread might not be met.
    2. .yield() is much based on the Thread Priorities concept. (All thread are assigned priorities and when a thread of higher priority is in Runnable state it ususally preempts / stops execution of lower priority threads depending on implementation of ThreadScheduler.)

    enter image description here Note:

    • both Thread.sleep() and Thread.yield() are static functions and affect the current thread executing it.
    • both the functions will not let go the synchronized locks they hold.
    0 讨论(0)
提交回复
热议问题