我原来用线程的时候都是用其中的一个 前两天发现
public class Test extends Thread implements Runnable
{
public void run()
{
System.out.println("run");
}
public static void main(String args[])
{
Thread t=new Thread(new Test());
t.start();
}
}
运行很正常 会打印出一个 run
请教高手
public class Test extends Thread implements Runnable
{
public void run()
{
System.out.println("run");
}
public static void main(String args[])
{
Thread t=new Thread(new Test());
t.start();
}
}
运行很正常 会打印出一个 run
请教高手
Test test=new Test();
test.start();
也可以实现同样功能。
如果用Thread t=new Thread(new Test());
t.start();用到的是Runnable接口。
如果用Test test=new Test();
test.start();用到的是继承Thread
还有,你要看到new Thread(Runnable r)
是以Runnable为引用的,也就是说只 要你实现了Runnable的接口就可以做为参数传入了
哪怕你这个类是继承自Thread的
start方法是Thread的
实现runable接口的时候也是用的Thread的start方法