Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invoking notify()

 
Akash Kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
notify();
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.
or
the purpose of invoking notify() in method doIt() is something else.
Thanks in advance
Akash
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
-Barry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic