More on Synchronization
Sometimes while a thread is executing a synchronized method, it wants to wait for some condition to become true. Therefore, it may need to give up the lock on the synchronized method for a time.
This is implemented by the monitor is associated with the instance of the class; it has a lock and a queue.
The queue holds all threads waiting to execute a synchronized method.
- A thread enters the queue by calling wait() inside the method or when another thread is already executing the method.
- When a synchronized method returns, or when a method calls wait(), another thread may access the object.
- As always, the scheduler chooses the highest-priority thread among those in the queue.
- If a thread is put into the queue by calling wait(), it can't be scheduled for execution until some other thread calls notify().