Class BlockingBuffer
Figures 23.14–23.15 demonstrate a Producer and a Consumer accessing an ArrayBlockingQueue. Class BlockingBuffer (Fig. 23.14) uses an ArrayBlockingQueue object that stores an Integer (line 7). Line 11 creates the ArrayBlockingQueue and passes 1 to the constructor so that the object holds a single value to mimic the UnsynchronizedBuffer example in Fig. 23.12. Lines 7 and 11 (Fig. 23.14) use generics, which we discussed in Chapters 16–20. We discuss multiple-element buffers in Section 23.8. Because our BlockingBuffer class uses the thread-safe ArrayBlockingQueue class to manage all of its shared state (the shared buffer in this case), BlockingBuffer is itself thread safe, even though we have not implemented the synchronization ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access