Java 当我试图重写run方法时显示错误
包com.jspiders.thread 公共类Mthrd1扩展线程{ {Java 当我试图重写run方法时显示错误,java,Java,包com.jspiders.thread 公共类Mthrd1扩展线程{ { }这是重写创建线程的run方法的有效方法: class Test extends Thread { public void run() { System.out.println("Thread Started and is Running"); } public static void main(String args[]) { Test t=new Test(); t.s
}这是重写创建线程的run方法的有效方法:
class Test extends Thread
{
public void run()
{
System.out.println("Thread Started and is Running");
}
public static void main(String args[])
{
Test t=new Test();
t.start();
}
}
还有另一种在java中通过实现runnable接口创建线程的方法。在您发布的代码中,您显式地调用run方法,而要调用Thread类的run方法,您需要调用start()方法隐式调用线程的run方法,您的程序显示错误,因为您显式调用run方法,并且在类中没有名为run的方法。您应该从最基本的开始
class Test extends Thread
{
public void run()
{
System.out.println("Thread Started and is Running");
}
public static void main(String args[])
{
Test t=new Test();
t.start();
}
}