File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify()" Watch "notify()" New topic
Author

notify()

Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
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

Joined: Feb 18, 2004
Posts: 96
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.


Rich
SCJP 1.4
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
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.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
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

Joined: Jul 21, 2003
Posts: 522
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

Joined: Feb 16, 2004
Posts: 50
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


SCJP 1.4
Suresh Thota
Ranch Hand

Joined: Sep 24, 2003
Posts: 152
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


SCJP 1.6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify()