Monitor Object
The Monitor Object design pattern synchronizes concurrent method execution to ensure that only one method at a time runs within an object. It also allows an object’s methods to schedule their execution sequences cooperatively. This pattern is also known as Thread-Safe Passive Object.
Requirements
If many threads access a shared object concurrently, the following requirements must be fulfilled.
- Due to the concurrent access, the shared object must be protected from non-synchronised read and write operations to avoid data races.
- The necessary synchronisation should be part of the implementation and not part of the interface.
- When a thread is done with the shared object, a notification should be triggered so that the next thread can use the shared object. This mechanism helps to avoid deadlocks and improves the overall performance of the system.
- After a method was performed, the invariants of the shared object must hold.
The solution to these four requirements...