Hi Nigel,
notify() belongs to java.lang.Object (and not to java.lang.Thread) - besides, every Object has a built-in lock which comes into play when a synchronized method (or code block) is entered by a Thread. Now, such thread owns the lock on this object, provided that the synchronized method is not static (otherwise, the thread would have a class lock rather than an object lock).
The notify() is related to a so-called monitor, notifying ONE waiting thread that the lock (monitor object) will be released "very soon" (please take into account that the lock is definitely released
exactly when the synchronized method (or code block) has finished - after this, the waiting thread (which has invoked "wait()" before) can continue its work - generally elaborating the data coming from the monitor.
It's crucial to notice that both synchronized blocks (where wait() and notify() are taking place) are synchronized on the same object, for example
- synchronized (
this) { ....
this.notify(); } and
- synchronized (
obj) { ...
obj.wait(); ...}
(where "this" and "obj" are both references to the same instance of a class - otherwise, you would get an IllegalMonitorException!)
As soon as wait() is invoked, the Object (for example "obj") is provided with a list of all waiting threads (waiting for obj) - so the notify() method can only notify such threads which are at the list. Nevertheless, if there is more than one thread waiting for a notification, there's no guarantee WHICH thread will get the notification. If you want to notify all waiting threads (for obj), use notifyAll() instead of notify().
If it's sounds too complicated, please read
SCJP 6 (K&B), page 746-752, "Thread Interaction".
Hope this helps!
P.S.: I've noticed too late that I've misinterpreted your question because of your headline. As you can see, my explanation goes into the direction "notify() without argument or target thread" ... indeed, use notifyAll() when it deals with more than one waiting thread