While using the wait
and notify
or notifyAll
methods in Java the following things must be remembered:
notifyAll
instead of notify
if you expect that more than one thread will be waiting for a lock. wait
and notify
methods must be called in a synchronized context. See the link for a more detailed explanation. wait()
method in a loop because if multiple threads are waiting for a lock and one of them got the lock and reset the condition, then the other threads need to check the condition after they wake up to see whether they need to wait again or can start processing. wait()
and notify()
method; every object has its own lock so calling wait()
on object A and notify()
on object B will not make any sense.