26.8 Producer/Consumer Relationship: Bounded Buffers
The program in Section 26.7 uses thread synchronization to guarantee that two threads manipulate data in a shared buffer correctly. However, the application may not perform optimally. If the two threads operate at different speeds, one them will spend more (or most) of its time waiting. For example, in the program in Section 26.7 we shared a single integer variable between the two threads. If the Producer
thread produces values faster than the Consumer
can consume them, then the Producer
thread waits for the Consumer
, because there are no other locations in the buffer in which to place the next value. Similarly, if the Consumer
consumes values faster than the Producer
produces them, the ...
Get Java How to Program (early objects), 9/e 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.