Thread.sleep() 的两个方法
¶线程休眠的多种方法
当想要让当前进程暂时歇个几百毫秒,肯定见过 Thread.sleep(long) 方法。在各种模拟线程长时间工作的代码中时常见到。不过我看到他们有好几个变体
public class Thread implements Runnable {
public static native Thread currentThread();
public static native void sleep(long millis) throws InterruptedException;
public static void sleep(long millis, int nanos) throws InterruptedException;
}
public enum java.util.concurrent.TimeUnit {
public void sleep(long timeout) throws InterruptedException;
}
// 让当前线程休眠3秒
TimeUnit.SECONDS.sleep(3);
Thread.currentThread().sleep(3000);
Thread.sleep(3000);

