虚假唤醒会影响Thread.sleep吗?
问题描述:
虚假唤醒会影响对Thread.sleep(x)的调用吗?显然,计时器不是100%精确的(导致唤醒时间略有不正确),但是它会受到虚假唤醒问题的影响吗?
Do spurious wakeups affect calls to Thread.sleep(x)? Obviously, the timer is not 100% precise (leading to minor inaccuracies in wakeup times), but is it affected by the spurious wakeup problem?
答
You're asking whether Thread.sleep()
is affected by the same spurious wakeup issue that is documented to affect Object.wait(long)
, right? The answer is that there is no documented spurious wakeup associated with Thread.sleep()
. You're right that no hard guarantees are made about exactly how long you'll sleep when you request N milliseconds. Also, of course, Thread.sleep()
terminates on thread interrupt.