相关疑难解决方法(0)

为什么notifyAll()在Integer上同步时会引发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");
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

Exception in thread "main" java.lang.IllegalMonitorStateException
        at java.lang.Object.notifyAll(Native Method)
        at test.main(test.java:6)
Run Code Online (Sandbox Code Playgroud)

java synchronization illegalmonitorstateexcep notify

38
推荐指数
1
解决办法
3万
查看次数