How to do it...

Follow these steps to implement the example:

  1. Create a class named EventStorage. It has two attributes, namely an int attribute called maxSize and a List<Date> attribute called storage:
         public class EventStorage {            private int maxSize;           private Queue<Date> storage;
  1. Implement the constructor of the class that initializes the attributes of the class:
        public EventStorage(){           maxSize=10;           storage=new LinkedList<>();         }
  1. Implement the synchronized method set() to store an event in storage. First, check whether storage is full or not. If it's full, it calls the wait() method until it has empty space. At the end of the method, we call the notify() method to wake up all the threads that are sleeping in the wait() method. In ...

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.