... 13–14 create two Conditions using Lock method newCondition:

  • Condition canWrite contains a queue for a Producer thread waiting while the buffer is full (i.e., there’s data in the buffer that the Consumer has not read yet). If the buffer is full, the Producer calls method await on this Condition. When the Consumer reads data from a full buffer, it calls method signal on this Condition.

  • Condition canRead contains a queue for a Consumer thread waiting while the buffer is empty (i.e., there’s no data in the buffer for the Consumer to read). If the buffer is empty, the Consumer calls method await on this Condition. When the Producer writes to the empty buffer, it calls method signal on this Condition.

The int variable buffer (line 16) holds the shared ...

Get Java How to Program, Early Objects, 11th 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.