This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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!!
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic