下面是Thread.sleep(long millis, int nanos)
的源代码。它根本不休眠nanos
。它仅调用sleep(millis)
。为什么JDK引入了这种误导性方法?
public static void sleep(long millis, int nanos)
throws InterruptedException {
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
millis++;
}
sleep(millis);
}