The signal is like the old tree falling in the forest. The noise makes no difference if
no one is there to hear it.
Suppose that a monitor called PC (for Producer??“Consumer) is available to support a set of processes that cooperate
producing items (e.g., product orders) and consuming them (e.g., making entries in shipment schedules for different
distribution centers). The monitor has two methods, addOrder and retrieveOrder; addOrder takes
an Order as an argument, and retrieveOrder returns an Order. The application code becomes very simple:
Producer:
while( true ) {
Order newOrder = createOrder( orderNumber );
PC.addOrder( newOrder );
}
108 OPERATING SYSTEMS [CHAP. 6
Consumer:
while( true ) {
Order thisOrder = PC.retrieveOrder( );
distributeOrder( thisOrder );
}
The complexity of guaranteeing mutual exclusion and proper coordination is hidden in the monitor??™s code. While
a monitor may require some thought to create, it greatly simplifies the rest of the code for the cooperating processes.
Java provides a simple and general-purpose monitor feature. Every object in Java has an associated monitor
that is entered by a thread when the thread enters code that is synchronized on the object. If a thread attempts
to enter synchronized code, and another thread already occupies the object??™s monitor, the thread blocks.
Pages:
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297