This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang. IllegalMonitorStateException: current thread not owner

 
vivek ja
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vivek ja
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic