[kaffe] Unexpected java.lang.IllegalMonitorStateException

Ito Kazumitsu ito.kazumitsu@hitachi-cable.co.jp
Sun Mar 28 22:28:02 2004


In message "[kaffe] Unexpected java.lang.IllegalMonitorStateException"
    on 04/03/24, Ito Kazumitsu <ito.kazumitsu@hitachi-cable.co.jp> writes:

> The behavior of the attached program when run on kaffe is
> different from the behavior when run on Sun's JDK.

I have made a simpler test program.

It seems that the problem occurs when "synchronized" is nested.

bash$ cat TestSync3.java
public class TestSync3 extends Thread {

        private static Object obj = new Object();
        public static void main(String[] args) throws Exception {
            int n = Integer.parseInt(args[0]);
            new TestSync3(0).start();
            Thread.sleep(2000);
            new TestSync3(n).start();
        }
        private int n;
        private TestSync3(int n) {
            this.n = n;
        }

        public void run() {
            switch(n) {
            case 0:
		synchronized(obj) {
                        try {
			    obj.wait();
                        }
                        catch (Exception e) {
                            throw new RuntimeException(e);
                        }
		}
                break;
            case 1:
		synchronized (obj) { obj.notifyAll(); }
                break;
            case 2:
		synchronized(obj) {
		synchronized (obj) { obj.notifyAll(); }}
                break;
            case 3:
		synchronized(obj) {
		synchronized(obj) {
		synchronized (obj) { obj.notifyAll(); }}}
                break;
            }
	}
}
bash$ java TestSync3 1
bash$ java TestSync3 2
java.lang.IllegalMonitorStateException
   at TestSync3.run (TestSync3.java:32)
bash$ java TestSync3 3
java.lang.IllegalMonitorStateException
   at TestSync3.run (TestSync3.java:37)