The Monitor Object design pattern synchronizes concurrent member function execution to ensure that only one member function runs within an object at a time. It also allows an object's member functions to schedule their execution sequences cooperatively.
by Rainer Grimm
From the article:
If many threads access a shared object concurrently, the following challenges exist.
- Due to the concurrent access, the shared object must be protected from non-synchronized read and write operations to avoid data races.
- The necessary synchronization should be part of the implementation, not the interface.
- When a thread is done with the shared object, a notification should be triggered so the next thread can use the shared object. This mechanism helps avoid and improves the system's overall performance.
- After the execution of a member function, the invariants of the shared object must hold.
A client (thread) can access the Monitor Object's synchronized member functions, and due to the monitor lock, only one synchronized member function can run at any given time. Each Monitor Object has a monitor condition that notifies the waiting clients.