java线程问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 19:02:46
请问这段程序:
public class Example implements Runnable{
public static void main(String[] args) {
Thread t=new Thread(new Example());
t.start();
}
public void run(int limit){
for(int x=0;x<limit;x++)
System.out.println(x);
}
}
为什么出错呢?说Example必须实现继承的抽象方法Runnable.run(),是说run中不能有参数么?谢谢了!

Runnable接口中有个run方法是空参的,如果你要实现Runnable个这接口那么你的类中就要实现这个接口中的所有方法,Runnable的run是空参的那么你也要有个空参的run方法。在调用start()方法的时候会自动执行无参run方法里面的代码。