getMessage();
System.out.println( newMessage );
}
}
}
Since every object in Java has an associated monitor, using a monitor in Java is very convenient. The
trick is to be sure that all the cooperating threads are synchronizing on the same object. A frequent error
by beginners is to have multiple threads synchronizing on different objects, which of course leads to failure
of synchronization. In the example above, the class PC_demo creates an instance of the monitor, and then
passes that same particular instance to both the Consumer and the Producer in the constructors of the
Consumer and the Producer. This insures that the Consumer and Producer are synchronizing on
the same object.
110 OPERATING SYSTEMS [CHAP. 6
DEADLOCK
In the multiprocessing, multithreaded environment, conditions can occur which produce a deadlock, a conflict
of needs and allocations that stops all computing. For instance, suppose processes A and B share access to files
X and Y, and assume the usual case that when a process opens a file for writing, the operating system gives that
process an exclusive lock on the file. Process A opens file X for writing, and then tries to open file Y for writing.
Process A cannot open file Y, however, because process B has file Y open for writing, so process A blocks.
Pages:
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300