java 的问题:这是用thread方法编程的,怎么用Runnable方法来继承??真的很想知道!!

来源:百度知道 编辑:UC知道 时间:2024/05/10 07:56:44
public class thread
{

public static void main(String args[])
{
SubThread thread1,thread2,thread3,thread4;
thread1=new SubThread("thread1",500);
thread2=new SubThread("thread2",400);
thread3=new SubThread("thread3",300);
thread4=new SubThread("thread4",200);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class SubThread extends Thread
{
private String currentThread;
private int sleepTime;

public SubThread(String name,int time)
{
currentThread=name;
sleepTime=time;
}

public void run()
{
try
{
sleep(sleepTime);

}
catch(InterruptedException e){}
System.out.println("This is "+currentThread+"! It's sleeptime is "+sleepTime+"!");
}

Runnable是接口,不能用于继承,而是实现,也就是implements Runnable,
如果你的代码要改的话应该将subThread后面的变为implements Runnable
在main方法中用thread1=new Thread(new subThread("thread1",500)); 来实例一个对象即可!!