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 VectorOutput: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(); } }
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 14There 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