我创建了一个名为Thread
implements的类,但由于某种原因Runnable
我无法调用start()
or方法。sleep()
每当我尝试这样做时,我都会收到错误消息,指出这些方法未为该类定义,并建议我创建它们。所以我创建了一个新项目并复制了一个示例代码,看看我自己的代码是否有问题,我收到了同样的错误。这是示例代码:
class Thread implements Runnable {
private int a;
public Thread (int a) {
this.a = a;
}
public void run() {
for (int i = 1; i <= a; ++i) {
System.out.println(Thread.currentThread().getName() + " is " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
}
这是我自己的代码:
public class Thread extends PID implements Runnable {
public Thread() {}; // Empty constructor for thread object
public void run() {
Random gen = new Random(); // Generates random values
int sleepTime; // Sleep time
sleepTime = gen.nextInt(60 - 1) + 1; // Generates random sleep time between 1 and 60 seconds
try {
Thread.sleep();
} catch (Exception e) {
System.out.println(e);
}
System.out.println("The thread has been terminated");
}
}