IllegalMonitorStateException on notify() when synchronized on an Integer

笑着哭i 提交于 2019-11-26 22:12:44

问题


I'm new to using wait() and notify() in Java and I'm getting an IllegalMonitorStateException.

Main Code

public class ThreadTest {

    private static Integer state = 0;
    public static void main(String[] args) {

        synchronized(state) {
            System.out.println("Starting thread");

            Thread t = new Thread(new AnotherTest());
            t.start();

            synchronized(state) {
                state = 0;
                while(state == 0) {
                    try {
                        state.wait(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("State is: " + state);
            }
        }
    }   

    public static class AnotherTest implements Runnable {

        @Override
        public void run() {
            synchronized(state) {
                state = 1;
                state.notify();
            }

        }

    }
}

I'm getting an IllegalMonitorStateException what state.notify() is called. Any ideas?

Edit: Based on answer below here is code that works. As a side note, I was first trying this with an enum which has the same problem of using Integer.

public class ThreadTest {

    private static int state = 0;
    private static Object monitor = new Object();
    public static void main(String[] args) {

        synchronized(monitor) {
            System.out.println("Starting thread");

            Thread t = new Thread(new AnotherTest());
            t.start();

            state = 0;
            while(state == 0) {
                try {
                    for(int i = 0; i < 5; i++) {
                        System.out.println("Waiting " + (5 - i) + " Seconds");
                        Thread.sleep(1000);
                    }
                    monitor.wait(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println("State is: " + state);
        }
    }   

    public static class AnotherTest implements Runnable {

        @Override
        public void run() {
            synchronized(monitor) {
                state = 1;
                monitor.notify();
            }

        }

    }
}

回答1:


This

private static Integer state = 0;

is equivalent to

private static Integer state = Integer.valueOf(0);

The invocation of valueOf(0) returns a reference to an Integer object, call it A.

You then do

synchronized(state) {

your thread acquires the lock on the object referenced by state, currently that is A.

You then do

state = 1;

which is equivalent to

state = Integer.valueOf(1);

which gives you a different reference to an Integer object, call it B, and assigns it to state. When you then call

state.notify();

you're invoking notify() on an object, B, for which your thread doesn't own the monitor. You can't call notify or wait on objects for which your thread doesn't own the monitor.



来源:https://stackoverflow.com/questions/25733718/illegalmonitorstateexception-on-notify-when-synchronized-on-an-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!