Are these two statement equivalent?
Thread.sleep(0);
Thread.yield();
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.