本文介绍了Thread.sleep(0)和Thread.yield()语句是否等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这两个陈述是否相同?
Thread.sleep(0);
Thread.yield();
推荐答案
不。最明显的区别是 sleep()
抛出(已检查) InterruptedException
。在实践中,效果可能几乎相同,但它完全依赖于实现。
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.
这篇关于Thread.sleep(0)和Thread.yield()语句是否等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!