5.3. 5.3 Monitor

A monitor is a synchronization mechanism based on the concept of an object that encapsulates shared variables. Inside the monitor, shared variables are tagged as private resources, so the only way to manipulate them is to call on methods of the interface that operate over the shared variables. This is therefore the only way to exchange data among two or more concurrent, parallel or distributed software components executing on a shared memory parallel platform. Mutual exclusion among them is implicit and guaranteed by the compiler, allowing only one software component to be active at a time inside the monitor – that is, executing one of the methods. No execution order can be assumed [Hoa74] [Bri75] [And91] [Bac93] [Har98] [HX98] [And00].

5.3.1. Example

The Java programming language can create and execute threads on the same processor or on different processors. To allow communications among threads, Java specifies the synchronized modifier. To implement a monitor as an object in Java, the synchronized modifier is used for all methods of a class in which only one thread should be executing at a time. These methods are normally declared as public and modify shared variables declared as private inside the monitor. The methods could also be declared as private, if public access to the monitor consists of calls to several of these synchronized methods. [Har98] [And00] [MSM04].

The current example describes the development of a synchronization mechanism based on the ...

Get Patterns for Parallel Software Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.