O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required