我正在與Thread
在Java
中工作,我得到以下錯誤 - 我不明白爲什麼?!線程「主」中的異常java.lang.IllegalMonitorStateException
代碼:
import java.util.Random;
public class Test {
public static void main(String[] args) throws InterruptedException {
Vlakno sude = new Vlakno("myName"); // Vlakno = thread class
sude.start();
sude.wait(); // ERROR IS ON THIS LINE
}
}
class Vlakno extends Thread {
private boolean canIRun = true;
private final String name;
Vlakno(String name) {
this.name = name;
}
@Override
public void run() {
while (canIRun) {
// System.out.println("Name: " + name);
}
}
public void mojeStop() {
System.out.println("Thread "+name +" end...");
this.canIRun = false;
}
}