虽然我已经在同步块中写了等待。我越来越IllegalMonitorStateException
。那是什么原因呢?
package trials;
public class WaitNotifyTrial {
public static void main(String[] args){
Generator g=new Generator();
g.start();
System.out.println("Start");
synchronized (g) {
try {
g.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("Printing exception");
e.printStackTrace();
}
System.out.println(g.value);
}
}
}
class Generator extends Thread{
int value;
public void run(){
synchronized (this) {
value=10;
}
notify();
}
}
}