This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

wait and notify  RSS feed

 
suri jagadish
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam having a problem with wait and notify methods
i am calling the wait() method on one thread form its own run method
and iam passing this referrence to other object and calling the notify method on this referrence and iam getting a illegal state excepetion

once a thread has called wait() on its self then only an external thread can call notify() on this thread so as to make it runnable. am i currect ? clarify...

thanking you
jagadish
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes sense that if a thread has called wait() it can't call notify on itself because it's not running any instructions. Another thread has to call the notify().

So we'd expect to see one thread wait() on an object and another thread notify() on the same object.

Can you describe your program a bit more to see if this is what's happening?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

That exception is thrown when you try to call wait() or notify() without owning the monitor on the object. In other words, you can call these methods only inside a synchronized method of that same object, or in a synchronized block that locks that object.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
That exception is thrown when you try to call wait() or notify() without owning the monitor on the object. In other words, you can call these methods only inside a synchronized method of that same object, or in a synchronized block that locks that object.


This is the short answer... the longer answer is... you need to understand why this is so. The designers of Java didn't require that the monitor be owned on a whim. There are some race conditions involved. Synchronizing the object will get you by this error message, but you still need to understand, and code it, so that you won't have the race condtion.

Henry
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by suri jagadish:
once a thread has called wait() on its self then only an external thread can call notify() on this thread so as to make it runnable. am i currect ? clarify...

thanking you
jagadish


You don't call wait on a thread you call it on a reference to an object. A thread can't wait on itself because a thread is not an object.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!