IllegalMonitorStateException

Hi All,

I know I am repeating this, but I could not find the solution on inernet.

I have following test program and it throws IllegalMonitorStateException. Not sure why it is throwing that.

public class WaitTest {

public static void main (String a[]) {

WaitTest wt = new WaitTest();

wt.doIt();

}

void doIt() {

try {

this.wait(60000);

}

catch (Exception e) {

System.out.println (e);

}

}\

}

thanks in advance

shishir

[530 byte] By [Patila] at [2007-10-3 3:18:59]
# 1
wait does not do what you think. You probably want Thread.sleep
IanSchneidera at 2007-7-14 21:10:51 > top of Java-index,Java Essentials,Java Programming...
# 2

Your current code gets IllegalMonitorStateException because you don't have the monitor on the object on which wait() is called. Read the API documentation.

And, try this:

public class WaitTest {

public static void main (String a[]) {

WaitTest wt = new WaitTest();

wt.doIt();

}

synchronized void doIt() {

try {

this.wait(1000);

}

catch (Exception e) {

System.out.println (e);

}

}

}

hiwaa at 2007-7-14 21:10:51 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks both of you. it helped. I got it why wait() was not working and how to acquire Objects Monitor.Shishir
Patila at 2007-7-14 21:10:51 > top of Java-index,Java Essentials,Java Programming...