Using Locks and Conditions to Implement Synchronization
We now implement the producer/consumer relationship using Lock
and Condition
objects to coordinate access to a shared single-element buffer (Figs. 23.20 and 23.21). In this case, each produced value is correctly consumed exactly once. Again, we reuse interface Buffer
and classes Producer
and Consumer
from the example in Section 23.5, except that line 28 is removed from class Producer
and class Consumer
.
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.