读书人

关于Thread种为什么Thread没有实例化

发布时间: 2013-09-07 14:12:44 作者: rapoo

关于Thread类,为什么Thread没有实例化就可以执行Thread.sleep(10000);


import java.lang.*;
import java.util.Date;

public class test
{
public static void main(String[] args)
{
Runner r=new Runner();
Thread t=new Thread(r);
t.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
}

class Runner implements Runnable
{
public void run()
{
while(true){
System.out.println("==========" +new Date()+"==========");
try {
Thread.sleep(1000);

} catch (InterruptedException e) {
return ;
}
}
}
}


为什么Thread没有实例化就可以执行Thread.sleep(10000);
[解决办法]
因为是static方法。
[解决办法]
静态方法啊
[解决办法]
   /**
* 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;



就像String.valueOf

读书人网 >J2SE开发

热点推荐