Search code examples
javawaitnotifyillegalmonitorstateexcep

IllegalMonitorStateException on notify() when synchronized on an Integer


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();
            }

        }

    }
}

Solution

  • 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.