Synchronization is Implemented by Monitors
An object that can block threads and notify them when it is available is called a monitor. A monitor is associated with an instance of the class; it has a lock and a queue.
If a class has one or more synchronized methods, each object (instance) of the class has a monitor. 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().