Monitor object pattern

The monitor object pattern is another concurrency design pattern that helps in the execution of multi-threaded programs. It is a design pattern implemented to make sure that at a single time interval, only one method runs in a single object, and for this purpose, it synchronizes concurrent method execution.

Unlike the active object design pattern, the monitor object pattern does not have a separate thread of control. Every request received is executed in the thread of control of the client itself, and until the time the method returns, the access is blocked. At a single time interval, a single synchronized method can be executed in one monitor.

The following solutions are offered by the monitor object pattern:

  • The synchronization boundaries are defined by the interface of the object, and it also makes sure that a single method is active in a single object.
  • It must be ensured that all the objects keep a check on every method that needs synchronization and serialize them transparently without letting the client know. Operations, on the other hand, are mutually exclusive, but they are invoked like ordinary method calls. Wait and signal primitives are used for the realization of condition synchronization.
  • To prevent the deadlock and use the concurrency mechanisms available, other clients must be allowed to access the object when the method of the object blocks during execution.
  • The invariants must always hold when the thread of control is interrupted voluntarily by the method.

Let's see the following diagram, which illustrates more about the monitor object design pattern in the concurrency application:

In this preceding diagram, the client object calls the monitor object that has several synchronized methods and the monitor object associated with the monitor conditions and monitor locks. Let's explore each component of this concurrency design pattern as follows:

  • Monitor object: This component exposes the methods that are synchronized to the clients
  • Synchronized methods: The thread-safe functions that are exported by the interface of the object are implemented by these methods
  • Monitor conditions: This component along with the monitor lock decides whether the synchronized method should resume its processing or suspend it

The active object and the monitor object patterns are the branches of design patterns of concurrency.

Now, the other type of concurrency patterns that we will discuss are the branches of architectural patterns for concurrency.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset