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 this case, we will ignore InterruptedException. In a real implementation, you must think what treatment you must give to them. You can rethrow or transform them into a different type of exception of the application:
        public synchronized void set(){ 
while (storage.size()==maxSize){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storage.offer(new Date());
System.out.printf("Set: %d",storage.size());
notify();
}
  1. Implement the synchronized method get() to get an event for storage purposes. First, check whether storage has events or not. If it has no events, it calls the wait() method until it is given some events. At the end of the method, we call the notifyAll() method to wake up all the threads that are sleeping in the wait() method. In this case, we will ignore InterruptedException. In a real implementation, you must think what treatment you must give to them. You can rethrow or transform them into a different type of exception of the application:
        public synchronized void get(){ 
while (storage.size()==0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String element=storage.poll().toString();
System.out.printf("Get: %d: %s ",storage.size(),element);
notify();

}
  1. Create a class named Producer and specify that it implements the Runnable interface. It will implement the producer of the example:
        public class Producer implements Runnable {
  1. Declare an EventStore object and implement the constructor of the class that initializes this object:
        private EventStorage storage; 

public Producer(EventStorage storage){
this.storage=storage;
}
  1. Implement the run() method that calls the set() method of the EventStorage object 100 times:
        @Override 
public void run() {
for (int i=0; i<100; i++){
storage.set();
}
}
  1. Create a class named Consumer and specify that it implements the Runnable interface. It will implement the consumer of the example:
        public class Consumer implements Runnable {
  1. Declare an EventStorage object and implement the constructor of the class that initializes this object:
        private EventStorage storage; 

public Consumer(EventStorage storage){
this.storage=storage;
}
  1. Implement the run() method. It calls the get() method of the EventStorage object 100 times:
        @Override 
public void run() {
for (int i=0; i<100; i++){
storage.get();
}
}
  1. Create the main class of the example by implementing a class named Main and adding the main() method to it:
        public class Main { 

public static void main(String[] args) {
  1. Create an EventStorage object:
        EventStorage storage=new EventStorage();
  1. Create a Producer object and Thread to run it:
        Producer producer=new Producer(storage); 
Thread thread1=new Thread(producer);
  1. Create a Consumer object and Thread to run it:
        Consumer consumer=new Consumer(storage); 
Thread thread2=new Thread(consumer);
  1. Start both the threads:
        thread2.start(); 
thread1.start();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.8.212