- A type, or abstract data type, encapsulates private data with public methods to operate on that data. A monitor type presents a set of programmer-defined operations that are provided mutual exclusion within the monitor.
- The monitor type also contains the declaration of variables whose values define the state of an instance of that type, along with the bodies of procedures or functions that operate on those variables. The syntax of a monitor is shown in Fig. 14.
Figure 14:
Syntax of a monitor.
|
- The representation of a monitor type cannot be used directly by the various processes. Thus, a procedure defined within a monitor can access only those variables declared locally within the monitor and its formal parameters.
- Similarly, the local variables of a monitor can be accessed by only the local procedures.
- The monitor construct ensures that only one process at a time can be active within the monitor. Consequently, the programmer does not need to code this synchronization constraint explicitly (see Fig. 15).
Figure 15:
Schematic view of a monitor.
|
- A solution to the producer-consumer problem using monitors in Java is given in Fig. 16.
Figure 16:
An outline of the producer-consumer problem with Java.
|
Cem Ozdogan
2010-04-06