views:

302

answers:

2

When i specifiy the time interval zero

static void Main()
{
   Thead.Sleep(0);
}

would the Main() thread give up the CPU resource it acquired or without waiting will it continue?

+5  A: 

Yes. The thread shall be suspended for that cycle allowing other threads access to the CPU. The code will be immediately scheduled for the next cycle.

The thread will continue being processed on it's next scheduled CPU slot in a multithreaded environment.

Thread.Sleep(-1) will suspend the thread indefinitely.

ChrisBD
+4  A: 

Thread.Sleep(0) gives up CPU resources only to the threads with equal or higher priority. If there is thread awaiting with lower priority, it will be ignored.

This is why it is recommended to use Thread.Sleep(1).

Vitaliy Liptchinsky
Thanks Vitaliy for your answer