为什么在对Integer对象进行同步时,notifyAll()会引发IllegalMonitorStateException异常?

7 浏览
0 Comments

为什么在对Integer对象进行同步时,notifyAll()会引发IllegalMonitorStateException异常?

为什么这个测试程序会导致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)

0