一个线程类休眠1000毫秒和一个线程对象休眠1000毫秒有什么联系吗 只有线程类及其子类对象才可以调用sleep方法吗
Thread.sleep(1000);
Thread thread =new Thread ( );
thread.sleep(1000);
本质上sleep是Thread类的静态方法,这两行代码是一样的含有,但是第二种thread.sleep的调用会报警告的:The static method sleep(long) from the type Thread should be accessed in a static way。
sleep是静态方法,应该直接使用Thread.sleep()来调用的。
直接Thread.sleep 为什么还用new一个呢。
最后都是调用的。
/**
* 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;
你说的例子是一个意思,sleep是一个静态方法,Thread可以直接调用