sleep 的源码:java
pub1ic class Thread imp1 ements Runnable {
//native 表示调用操做系统底层实现的方法 pub1ic static native void sleep(1ong millis) throws InterruptedException ; pub1ic static void s1eep(1ong millis, int nanos) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0|I nanos > 999999) { throw new Il1egalArgumentExcepti on( "nanosecond timeout value out of range"); } if (nanos >= 500000 II (nanos != 0 && millis == 0)) { milliS++; } sleep(mi1lis); } //... }
public class Object { public final native void wait(long timeout) throws InterruptedException; 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); } //... }