23.7 (Advanced) Producer/Consumer Relationship with synchronized, wait, notify and notifyAll

[Note: This section is intended for advanced programmers who want to control synchronization.2] The previous example showed how multiple threads can share a single-element buffer in a thread-safe manner by using the ArrayBlockingQueue class that encapsulates the synchronization necessary to protect the shared mutable data. For educational purposes, we now explain how you can implement a shared buffer yourself using the synchronized keyword and methods of class Object. Using an ArrayBlockingQueue generally results in more-maintainable, better-performing code.

After identifying the shared mutable data and the synchronization policy (i.e., associating ...

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.