A thread can use Object.wait() to block until another thread calls notify() or notifyAll() on that object.
But what if a thread wants to wait until one of multiple objects is signaled? For example, my thread must wait until either a) bytes become available to read from an InputStream or b) an item is added to an ArrayList.
How can the thread wait for either of these events to occur?
EDIT
This question deals with waiting for multiple threads to complete — my case involves a thread waiting for one of many objects to be singnaled.
A thread cannot wait on more than one object at a time.
The
wait()andnotify()methods are object-specific. Thewait()method suspends the current thread of execution, and tells the object to keep track of the suspended thread. Thenotify()method tells the object to wake up the suspended threads that it is currently keeping track of.Useful link : Can a thread call wait() on two locks at once in Java (6) ?