Are Thread.sleep(0) and Thread.yield() statements equivalent?

java, multithreading

Solution

No. The most obvious difference is that `sleep()` throws the (checked) `InterruptedException`. In practice, the effect may be almost the same, but it's entirely implementation-dependant.

I'd wager that doing each a million times in a row would take much longer for sleep(), since system timer granularity probably often causes it to actually sleep for a non-negligible amount of time.

Problem

Are these two statement equivalent? ``` Thread.sleep(0); Thread.yield(); ```

Original source

Related problems