Bounded Buffers Using ArrayBlockingQueue

The simplest way to implement a bounded buffer is to use an ArrayBlockingQueue for the buffer so that all of the synchronization details are handled for you. This can be done by modifying the example from Section 23.6 to pass the desired size for the bounded buffer into the ArrayBlockingQueue constructor. Rather than repeat our previous ArrayBlockingQueue example with a different size, we instead present an example that illustrates how you can build a bounded buffer yourself. Again, using an ArrayBlockingQueue will result in moremaintainable and better-performing code. In Exercise 23.13, we ask you to reimplement this section’s example, using the Java Concurrency API techniques presented in Section 23.9 ...

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.