Tuesday, March 21, 2017

Producer Consumer problem using wait() and notify() methods

As per the wiki,In computer,The Producer-Consumer problem also known as the Bounded-buffer problem is classic example of a multi-process synchronization problem. The problem describes two processes.The producer and the Consumer,Who share a common,fixed size buffer used as a queue,The producer's job is to produce/generate the data,put it into buffer,and start again.At the same time,the consumer is consuming the data (i.e removing it from buffer), one peice at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer

The solution for the Producer is to either go to sleep or discard if the buffer is full.Next time the consumer removes an item from the buffer,it notifies the Producer,who starts to fill the buffer again.In the same way,the consumer can go to sleep if it finds the buffer to be empty.The next time the producer puts data into the buffer,its wakes up the sleeping consumer. The solution can be reached by means of inter process communication.

Java program to solve the Producer consumer problem in java 

Here is complete java program to solve the Producer consumer problem in java programming language.In the program we have using wait() and notify methods from java.lang.Object class.
import java.util.Vector;
class Producer implements Runnable {
  private Vector sharedQueue;
  private int MAX_VALUE = 5;
  private int i = 0;

  public Producer(Vector sharedQueue) {
    super();
    this.sharedQueue = sharedQueue;
  }

  @Override
  public void run() {
    while (true) {
      try {
        produce(i++);
      } catch (InterruptedException ex) {
        ex.printStackTrace();
      }
    }
  }

  public void produce(int i) throws InterruptedException {
    synchronized (sharedQueue) {
      while (sharedQueue.size() == MAX_VALUE) {
        System.out.println("SharedQueue is full.Waiting for Consumer  to consume the object");
        sharedQueue.wait();
      }
    }
    synchronized (sharedQueue) {
      System.out.println("Produce the produced element " + i);
      sharedQueue.add(i);
      Thread.sleep(1000);
      sharedQueue.notify();
    }
  }
}

class Consumer implements Runnable {
  private Vector sharedQueue;

  public Consumer(Vector sharedQueue) {
    super();
    this.sharedQueue = sharedQueue;
  }

  @Override
  public void run() {
    while (true) {
      try {
        consume();
      } catch (InterruptedException ex) {
        ex.printStackTrace();
      }
    }
  }
  private void consume() throws InterruptedException {
    synchronized (sharedQueue) {
      while (sharedQueue.isEmpty()) {
        System.out.println("SharedQueue is empty.Waiting for Producer to produce the Object");
        sharedQueue.wait();
      }
    }
    synchronized (sharedQueue) {
      Thread.sleep(500);      
      System.out.println("Consumed the element :" + sharedQueue.remove(0));
      sharedQueue.notify();
    }
  }
}

public class ProducerConsumerManager {
  public static void main(String[] args) {
    Vector sharedQueue = new Vector<>();
    Thread producer = new Thread(new Producer(sharedQueue));
    Thread consumer = new Thread(new Consumer(sharedQueue));
    producer.start();
    consumer.start();
  }
}
Output:
Produce the produced element 0
Produce the produced element 1
Consumed the element :0
Consumed the element :1
SharedQueue is empty.. Waiting for Producer to produce the Object
Produce the produced element 2
Produce the produced element 3
Produce the produced element 4
Produce the produced element 5
Produce the produced element 6
SharedQueue is full... Waiting for Consumer  to consume the object
Consumed the element :2
Consumed the element :3
Consumed the element :4
Consumed the element :5
Consumed the element :6
SharedQueue is empty.. Waiting for Producer to produce the Object
Produce the produced element 7
Produce the produced element 8
Produce the produced element 9
Produce the produced element 10
Produce the produced element 11
SharedQueue is full... Waiting for Consumer  to consume the object
Consumed the element :7
Consumed the element :8
Consumed the element :9
Consumed the element :10
Produce the produced element 12
Produce the produced element 13
Produce the produced element 14
There are many other way to solve the Producer Consumer problem using java .We will discuss upcoming blogs.
That's all about Producer Consumer problem using wait() and notify() methods.Please write your valuable comments below .

No comments:

Post a Comment