locked
What's the difference between Thread.Sleep(0) and Thread,Yield()? RRS feed

  • Question

  • Hi all,

    I want to know what's the difference between Thread.Sleep(0) and Thread.Yield(), and what's the scenario to use them? I would appreciate if you can provide some examples. 

    Thanks

    Monday, August 6, 2012 9:47 AM

Answers

  • Hi! Microsoft says:

    Thread.Yeld method causes the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the thread to yield to.

    Thread.Sleep(msec) blocks the current thread for the specified number of milliseconds.

    In other words, yield just says, end my timeslice prematurely, look around for other threads to run. If there is nothing better than me, continue.

    Sleep says I don't want to run for x milliseconds. Even if no other
    thread wants to run, don't make me run.

    For example:

    for (int i = 0; i < 5; i++)
    {
       Console.WriteLine("Sleep for 2 seconds.");
       Thread.Sleep(2000);
    }

    Sergio Meloni, MSP
    Twitter / Facebook / Blog


    • Edited by supermelo Monday, August 6, 2012 9:58 AM
    • Proposed as answer by Kapul Bhatnagar Monday, August 6, 2012 6:07 PM
    • Marked as answer by Doraemon_3 Tuesday, August 7, 2012 5:51 AM
    Monday, August 6, 2012 9:53 AM
  • To complete what other colleages said, there are 3 differences between Thread.Yield() and Thread.Sleep(0)

    • after calling Thread.Yield(), time-slice is given only to those threads who are running at the same processor with current thread.
    • Thread.Yield() has a boolean return value which indicates that whether there were any threads who has received time-slice or not, but Thread.Sleep() has not return value.
    • Thread.Sleep(0) prevents time-slice being handed over to threads with lower priorities. So it is more prone to starvation. But Thread.Yield() doesn't care to thread priorities, so starvation is less to happen.
    • Proposed as answer by Kapul Bhatnagar Monday, August 6, 2012 6:07 PM
    • Marked as answer by Doraemon_3 Tuesday, August 7, 2012 5:51 AM
    Monday, August 6, 2012 3:41 PM

All replies

  • Hi! Microsoft says:

    Thread.Yeld method causes the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the thread to yield to.

    Thread.Sleep(msec) blocks the current thread for the specified number of milliseconds.

    In other words, yield just says, end my timeslice prematurely, look around for other threads to run. If there is nothing better than me, continue.

    Sleep says I don't want to run for x milliseconds. Even if no other
    thread wants to run, don't make me run.

    For example:

    for (int i = 0; i < 5; i++)
    {
       Console.WriteLine("Sleep for 2 seconds.");
       Thread.Sleep(2000);
    }

    Sergio Meloni, MSP
    Twitter / Facebook / Blog


    • Edited by supermelo Monday, August 6, 2012 9:58 AM
    • Proposed as answer by Kapul Bhatnagar Monday, August 6, 2012 6:07 PM
    • Marked as answer by Doraemon_3 Tuesday, August 7, 2012 5:51 AM
    Monday, August 6, 2012 9:53 AM
  • thread.yield() --- If this method succeeds, the rest of the thread's current time slice is yielded. The operating system schedules the calling thread for another time slice, according to its priority and the status of other threads that are available to run.

    thread.sleep() --- If this method suceeds,this thread was suspended. Next available thread starts execution.

    Monday, August 6, 2012 10:26 AM
  • Sleep(0) lets other threads run, but then returns without regard to the time slice.  Sleep(1) relinquishes the time slice.
    Monday, August 6, 2012 1:44 PM
  • To complete what other colleages said, there are 3 differences between Thread.Yield() and Thread.Sleep(0)

    • after calling Thread.Yield(), time-slice is given only to those threads who are running at the same processor with current thread.
    • Thread.Yield() has a boolean return value which indicates that whether there were any threads who has received time-slice or not, but Thread.Sleep() has not return value.
    • Thread.Sleep(0) prevents time-slice being handed over to threads with lower priorities. So it is more prone to starvation. But Thread.Yield() doesn't care to thread priorities, so starvation is less to happen.
    • Proposed as answer by Kapul Bhatnagar Monday, August 6, 2012 6:07 PM
    • Marked as answer by Doraemon_3 Tuesday, August 7, 2012 5:51 AM
    Monday, August 6, 2012 3:41 PM