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

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

Are these two statement equivalent?

Thread.sleep(0);
Thread.yield();
12条回答
  •  离开以前
    2020-11-27 03:17

    No, they are not equivalent and besides the explanations above, I think it's necessary to check the Javadoc of yield. It seems not a good idea to use yield unless below situation meets.

     It is rarely appropriate to use this method. It may be useful
     for debugging or testing purposes, where it may help to reproduce
     bugs due to race conditions. It may also be useful when designing
     concurrency control constructs such as the ones in the
     {@link java.util.concurrent.locks} package.
    

提交回复
热议问题