Fields and Methods of Class SynchronizedBuffer
Class SynchronizedBuffer
contains fields buffer
(line 6) and occupied
(line 7)—you must synchronize access to both fields to ensure that class SynchronizedBuffer
is thread safe. Methods blockingPut
(lines 10–31) and blockingGet
(lines 34–54) are declared as synchronized
—only one thread can call either of these methods at a time on a particular SynchronizedBuffer
object. Field occupied
is used to determine whether it’s the Producer
’s or the Consumer
’s turn to perform a task. This field is used in conditional expressions in both the blockingPut
and blockingGet
methods. If occupied
is false
, then buffer
is empty, so the Consumer
cannot read the value of buffer
, but the Producer
can place a value into ...
Get Java™ How To Program (Early Objects), Tenth Edition 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.