• 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

notify()

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can i call the notify() and notifyAll() methods in a nonsynchronized method()?
If so, pls explain with an example.
Regards
Vineela
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vineela Devi Jakka:
Hi,
Can i call the notify() and notifyAll() methods in a nonsynchronized method()?
If so, pls explain with an example.
Regards
Vineela



Vineela,
These CAN be called in a non-synchronized method {B}but[/B] they MUST be called in a synchronized context....synchronized on the object you're calling the methods on....
For example

The key is that when the call is made to notify()/notifyAll() the thread making the call must own the lock on the object (which it obtains above via the synchronized(someObject) call. If the thread doesn't own the lock then the call will throw a IllegalMonitorStateException runtime exception.
Hope this helps.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vineela and Richard,
The answer to your question is YES, that is, you can call the notify() and notifyAll() methods in a nonsynchronized method(). However, i am not sure if this case is in the scope of the exam or not.
 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
so, then what will be the answer for this sort of Q.
notify() and notify() methods can be called only in synchronized methods.
Is the Ans for the above q is true or false?
Vineela
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vineela-
Almost every certification book I read states that notify() and notifyAll() must be called within a synchronized block. However, the scenario I raised is more like a twist, which you should not expect in the real exam.
However, if (and that is a big if) you get this question in the exam then you should probably answer it as "True", that is you can call notify() and notifyAll() from a synchronized block only.

That is at least my opinion.
[ April 01, 2004: Message edited by: Vicken Karaoghlanian ]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vicken
I think exactly the same!
we should just remember that notify and notifyAll should be called form synchronized context, the other cases are out of the exam scope
Fethi
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If ever this question is on the exam, it would be more clearly worded like this.
Can i call the notify() and notifyAll() methods in a nonsynchronized method() without causing compile time error/runtime exception?
Just my thoughts.
Cheers
-Suresh Thota
reply
    Bookmark Topic Watch Topic
  • New Topic