Other threads that want access to a synchronized object must wait for the first thread to release the object before they can continue. When a thread is waiting for another thread to release the lock on an object it is blocked.
Synchronization is a dangerous thing and should be avoided where possible. Two threads can each lock an object the other needs, and thus prevent both threads from running. This is called deadlock.
The current thread can release its hold on an object's monitor
by calling that object's wait()
method. The thread is then
suspended until the thread that then gets the object's monitor
calls notify()
or notifyAll()
.
When wait
is called, all locks on objects are
released. When wait returns, the lock on the object is regained.
This gives other threads the chance to finish what they're
doing.