public class Exercise {
public static void main(String[] args) {
Exercise ex=new Exercise();
ex.go();
}
public void go(){
MyThread mt=new MyThread("one");
mt.start();
}
}
class MyThread extends Thread{
private String name="";
MyThread(String s){
name=s;
}
public void run(){
notwait();
System.out.println("finished");
}
public void notwait(){
while(true){
try{
System.out.println("waiting");
wait();
}
catch(InterruptedException e){}
System.out.println(name);
}
}
}
这段程序运行后结果为
waiting
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at mypackage.MyThread.notwait(Exercise.java:34)
at mypackage.MyThread.run(Exercise.java:24)
请问导致此异常的具体原因。非常感谢! |