• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about wait() and notify()

 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My answer was that the code does not even compile because the call to notify()/wait() are not under a synchronized block. But it throws a runtime exception, which is fixed by adding the synchronized keyword to the doStuff() method.
So, when a wait()/notify()/notifyAll() are not placed under a synchronized block it'll throw a runtime exception, right? I just wanna make sure I'm concluding the right thing. There might be another reason and I haven't acknowledged ..yet..Any other observations will be highly appreciated.
thx
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres, the exception warning would be: java.lang.IllegalMonitorStateException: current thread not owner.
Which is the exception generated by code that uses wait() & notify() in non-synchronised blocks.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example to show that wait() and notify() do not have to be declared in synchronized methods. But the threads that execute wait() and notify() must hold the lock on the object.


[ June 22, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, right. Now, let�s describe what �placed under� means.
wait, notify and notifyAll may be invoked only by a thread that holds the
synchronization lock of the object on which the method is invoked.
The invocation can be made directly in a method or block of code declared as
synchronized, or can be made indirectly from a method invoked in such code.
Compliance generally cannot be verified at compile time. Failure to comply causes
these operations to throw an IllegalMonitorStateException at run time.
You can use the static method Thread.holdsLock(Object o) in your practice
programs to show you whether the current thread holds a lock.
[ June 22, 2003: Message edited by: Marlene Miller ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic