Thread.sleep Thread能够去掉
如果你自己定义的类继承了Thread类,那么Thread可以去掉,如果你自己定义的类使用了Runnable接口,那么Thread不能去掉,使用时只能这样使用Thread.sleep(),Thread.currentThread()等等。
//myThreadTest.java
class myThread extends Thread{
myThread(){
super();
}
public void run(){
System.out.println(currentThread());
System.out.println(currentThread().getName());
try{
sleep(1000);
}catch(InterruptedException e){;}
}
}
public class myThreadTest{
public static void main(String[] args){
myThread thread1=new myThread();
myThread thread2=new myThread();
thread1.start();
thread2.start();
}
}
//myRunnable.java
public class myRunnable implements Runnable{
Thread thread1,thread2;
public void run(){
System.out.println(Thread.currentThread());
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
public void start(){
thread1=new Thread(this);
thread2=new Thread(this);
thread1.start();
thread2.start();
}
public static void main(String[] args){
myRunnable my=new myRunnable();
my.start();
}
}