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 invoking notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "invoking notify() " Watch "invoking notify() " New topic
Author

invoking notify()

Akash Kumar
Greenhorn

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;
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

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.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: invoking notify()
 
Similar Threads
Displaying the largest and smallest of three integers
Derived Class function called twice...Why ?
a puzzling thread code.
draw a circle instead of Mandelbrot set
Project Euler : Problem 23