为什么 notifyAll() 在 Integer 上同步时会引发 IllegalMonitorStateException?
2022-09-01 02:33:18
为什么此测试程序导致 ?java.lang.IllegalMonitorStateException
public class test {
static Integer foo = new Integer(1);
public static void main(String[] args) {
synchronized(foo) {
foo++;
foo.notifyAll();
}
System.err.println("Success");
}
}
结果:
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at test.main(test.java:6)