Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

wait and notify

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
get schwifty. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic