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

后端 未结 12 1746
太阳男子
太阳男子 2020-11-27 02:42

Are these two statement equivalent?

Thread.sleep(0);
Thread.yield();
12条回答
  •  醉酒成梦
    2020-11-27 03:26

    The famous Brian Goetz's book "Java Concurrency in Practice" (published in 2006 but still fundamentally valid) says the following on this question.

    The semantics of Thread.yield and Thread.sleep(0) are undefined [JLS17.9]; the JVM is free to implement them as no-ops or treat them as scheduling hints. In particular, they are not required to have the semantics of sleep(0) on Unix systems — put the current thread at the end of the run queue for that priority, yielding to other threads of the same priority — though some JVMs implement yield in this way.

    The rest one can find in the Javadoc pages.

提交回复
热议问题