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

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

Are these two statement equivalent?

Thread.sleep(0);
Thread.yield();
12条回答
  •  一整个雨季
    2020-11-27 03:38

    This really depends on the platform and version of the JVM. For example, under Windows in JDK 5 (Hotspot), yield() is literally implemented as Sleep(0)-- although a sleep of 0 is treated slightly specially by Windows as I recall. But in JDK 6, yield() is implemented as SwitchToThread().

    I put together some information a while ago on Thread.yield(), including some implementational details that may be of interest. (You might also want to see the stuff on Thread.sleep() I put together on the same site.)

提交回复
热议问题