It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes invoking 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 "invoking notify() " Watch "invoking notify() " New topic

invoking notify()

Akash Kumar

Joined: Jul 17, 2002
Posts: 13
Hi All
Pls correct me if I am wrong in the follwing code:
public synchronized int doIt(int z) {
int x=5;
while(otherMethod()) {
try { wait(); }
catch(InterruptedException e) {}
for(int i=0;i<z;i++) count += x;
int result = count - x;
return result;
Using the code above, the purpose of invoking notify() in method doIt() is that it signals waiting threads so it they may attempt to acquire the object's monitor once the running thread has left doIt.
the purpose of invoking notify() in method doIt() is something else.
Thanks in advance
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Object.notify() wakes up one unspecified thread from those threads waiting on the object's monitor.
It's used to signal to the other threads that the notifier has finished working with the shared object and someone else can have a go.
Use Object.notifyAll() to signal to all waiting threads that they can now attempt to get hold of the shared object.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
jQuery in Action, 3rd edition
subject: invoking notify()
It's not a secret anymore!