• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notify()

 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • 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
 
Richard Quist
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
fethi makhlouf
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
 
Suresh Thota
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic