How it works...

The key to this example is the set() and get() methods of the EventStorage class. First of all, the set() method checks whether there is free space in the storage attribute. If it's full, it calls the wait() method to wait for free space. When the other thread calls the notify() method, this thread wakes up and checks the condition again. The notify() method doesn't guarantee that the condition is met. This process is repeated until there is free space in storage and it can generate a new event and store it.

The behavior of the get() method is similar. First, it checks whether there are events on the storage attribute. If the EventStorage class is empty, it calls the wait() method to wait for events. When the other thread ...

Get Java 9 Concurrency Cookbook - Second 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.