wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes java.lang. IllegalMonitorStateException: current thread not owner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "java.lang. IllegalMonitorStateException: current thread not owner" Watch "java.lang. IllegalMonitorStateException: current thread not owner" New topic
Author

java.lang. IllegalMonitorStateException: current thread not owner

vivek ja
Ranch Hand

Joined: Feb 24, 2005
Posts: 80
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
vivek ja
Ranch Hand

Joined: Feb 24, 2005
Posts: 80
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang. IllegalMonitorStateException: current thread not owner