Follow these steps to implement the example:
- 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;
- Implement the constructor of the class that initializes the attributes of the class:
public EventStorage(){ maxSize=10; storage=new LinkedList<>(); }
- 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 ...