- 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. 6.18.
Figure 6.18:
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. 6.19).
Figure 6.19:
Schematic view of a monitor.
|
- A solution to the producer-consumer problem using monitors in Java is given in Fig. 6.20.
Figure 6.20:
An outline of the producer-consumer problem with Java.
|
Cem Ozdogan
2011-02-14