sleep(long millis, int nanos)
thread类中的sleep方法没有实现ns级别的睡眠
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);
}
- 由jdk源码可知道,只是实现了四舍五入的功能
- 线程休眠的最小单位还是毫秒。
- 当第二个参数nanos大于0.5毫秒时或者小于1毫秒,进一位。
- 其余情况不进位。
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
- wait类似