java.lang. IllegalMonitorStateException: current thread not owner
vivek ja
Ranch Hand
Joined: Feb 24, 2005
Posts: 80
posted
0
When i use the wait(int) method in my program I am getting this error java.lang.IllegalMonitorStateException: current thread not owner java.lang.Object.wait(Native Method) FaxServlet.doPost(FaxServlet.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:709) javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
This is my code: if (status == -1 ||status == -2||status == -3 ) { wait(5000); } I am running the application in Tomcat 5 Pls help me out! thanks
Ernest Friedman-Hill
author and iconoclast
Marshal
You can only call wait() or notify() when you hold the lock on an object -- i.e., in a synchronized method of the object, or in a block synchronized on that object.
From the code, it is not clear that wait() is actually being used to wait for a notification from another thread. It looks as if maybe it is just being used to pause execution. If that is the case, then sleep() is better and does not require synchronisation on anything.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
vivek ja
Ranch Hand
Joined: Feb 24, 2005
Posts: 80
posted
0
Thanks! I am using Thread.sleep() now and it works.
wait() and sleep() do the same thing isnt it? Only differnce I can see is that wait can be called only inside a synchronized block or method, because the lock on the object should be held.
Ernest Friedman-Hill
author and iconoclast
Marshal
wait() waits until some other thread calls notify() on the same object; it's therefore a way for threads to communicate. If you pass a time to wait(), then it gives up after that amount of time. sleep(), on the other hand, just sleeps.