JDK中Thread.sleep(ms,ns)的作用是什么?

时间:2018-11-04 06:48:45

标签: java

下面是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);
}

0 个答案:

没有答案