javadoc说得相当明白了, sleep的方法的doc /** * Causes the currently executing thread to sleep (temporarily cease * execution) for the specified number of milliseconds, subject to * the precision and accuracy of system timers and schedulers. The thread * does not lose ownership of any monitors. * * @param millis the length of time to sleep in milliseconds. * @exception InterruptedException if any thread has interrupted * the current thread. The <i>interrupted status</i> of the * current thread is cleared when this exception is thrown. * @see Object#notify() */其实类似的还有好多, 例如Class.getResourceAsStream和ClassLoader.getResourceAsStream, 功能一样, 只是寻找的方法有点不一样而已
/** * Causes the currently executing thread to sleep (temporarily cease * execution) for the specified number of milliseconds, subject to * the precision and accuracy of system timers and schedulers. The thread * does not lose ownership of any monitors. * * @param millis the length of time to sleep in milliseconds. * @exception InterruptedException if any thread has interrupted * the current thread. The <i>interrupted status</i> of the * current thread is cleared when this exception is thrown. * @see Object#notify() */ public static native void sleep(long millis) throws InterruptedException; /** * Returns a reference to the currently executing thread object. * * @return the currently executing thread. */ public static native Thread currentThread(); 就是同一个对象,当你起一个main单线程的时候,就可以很容易发觉,你无论 currentThread(); 还是Thread,线程根本不会变多。跟简单点说。 public static void main(String args[]) { System.out.println(Thread.activeCount()); System.out.println(Thread.currentThread().getId()); System.out.println(Thread.activeCount()); } 输出都是1
在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。
static Thread currentThread()
返回对当前正在执行的线程对象的引用。
很明显没啥区别
其实还有一个类似的方法,就是java.util.concurrent下面的:
void sleep(long timeout)
使用此单元执行 Thread.sleep.这是将时间参数转换为 Thread.sleep 方法所需格式的便捷方法。
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds, subject to
* the precision and accuracy of system timers and schedulers. The thread
* does not lose ownership of any monitors.
*
* @param millis the length of time to sleep in milliseconds.
* @exception InterruptedException if any thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
* @see Object#notify()
*/其实类似的还有好多, 例如Class.getResourceAsStream和ClassLoader.getResourceAsStream, 功能一样, 只是寻找的方法有点不一样而已
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds, subject to
* the precision and accuracy of system timers and schedulers. The thread
* does not lose ownership of any monitors.
*
* @param millis the length of time to sleep in milliseconds.
* @exception InterruptedException if any thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
* @see Object#notify()
*/
public static native void sleep(long millis) throws InterruptedException; /**
* Returns a reference to the currently executing thread object.
*
* @return the currently executing thread.
*/
public static native Thread currentThread();
就是同一个对象,当你起一个main单线程的时候,就可以很容易发觉,你无论 currentThread();
还是Thread,线程根本不会变多。跟简单点说。 public static void main(String args[]) {
System.out.println(Thread.activeCount());
System.out.println(Thread.currentThread().getId());
System.out.println(Thread.activeCount());
}
输出都是1
Thread.sleep()是Thread类的方法,只对当前线程起作用,睡眠一段时间.
如果线程是通过继承Thread实现的话这2个方法没有区别;
如果线程是通过实现Runnable接口来实现的,则不是Thread类,不能直接使用Thread.sleep()
必须使用Thread.currentThread()来得到当前线程的引用才可以调用sleep(),
所以要用Thread.currentThread().sleep()来睡眠...
为什么?sleep()不是Thread类的静态方法么,为什么在实现不继承Thread就不能直接调用它的sleep()方法?