Originally posted by sai kumar:
Thx Rich,
Can you please throw more light on the wait(), notify() and notifyAll() methods. I am confused if these methods are to be called by the object or the Thread. How do u really use them. I understand that wait will cause a thread to wait until the object on which it is waiting indicates that it is modified.(Is notify called by thread or the object).
Thanks in advance.
bye
sk
They need to be called within a synchronized context (i.e. either a synchronized method or a synchronzed block of code) and the caller needs to have acquired the lock on the object that the wait()/notify()/notifyAll() method is being called on....
If you think about this, it really doesn't make sense to use these UNLESS you're working with multiple threads (otherwise you could just write your program to process the required steps sequentially and avoid all the "messiness")...presumably you're using the threads to improve performance (or improve "perceived" performance by making your application more responsive to the user and not making the user wait while you're doing other processing). You wind up calling them on some object within some number of threads....but remember that a Thread
is an Object, so there's no reason why you can't call the methods on a Thread object, if your application is designed that way....
The following psuedo-code might help