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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.