In previous post, discussed the Producer Consumer problem-solution using wait() and notify() methods. In this post, we will learn how to solve the Producer Consumer problem using BlockingQueue.
BlockingQueue is a queue(added in java.util.concurrent package) which is thread safe to insert or retrieve elements from it. And also it provides a mechanism which blocks request for inserting new elements when the queue is full and also blocks request for removing elements when the queue is empty. The classes that implementing BlockingQueue are ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue and PriorityBlockingQueue and so on.
Related Posts :
Producer Consumer Problem - Solution using wait and notify In Java
Thread(or Multithread) interview questions & answers
BlockingQueue is a queue(added in java.util.concurrent package) which is thread safe to insert or retrieve elements from it. And also it provides a mechanism which blocks request for inserting new elements when the queue is full and also blocks request for removing elements when the queue is empty. The classes that implementing BlockingQueue are ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue and PriorityBlockingQueue and so on.
Program to Solve the Producer-Consumer Problem using BlockingQueue :--
package com.pr; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class ProducerConsumer { public static void main (String[] args) { BlockingQueue<Integer> sharedQueue = new LinkedBlockingQueue<Integer>(); Producer producer = new Producer(sharedQueue); Consumer consumer = new Consumer(sharedQueue); Thread p = new Thread(producer, "Producer Thread"); Thread c = new Thread(consumer, "Consumer Thread"); p.start(); c.start(); } } class Producer implements Runnable { BlockingQueue<Integer> sharedQueue = new LinkedBlockingQueue<Integer>(); public Producer(BlockingQueue<Integer> sharedQueue) { this.sharedQueue = sharedQueue; } public void run() { for (int i = 1; i<=10; i++) { try { System.out.println("Produced - "+i); sharedQueue.put(i); Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { BlockingQueue<Integer> sharedQueue = new LinkedBlockingQueue<Integer>(); public Consumer(BlockingQueue<Integer> sharedQueue) { this.sharedQueue = sharedQueue; } @Override public void run() { while (true) { try { System.out.println("Consumed - "+sharedQueue.take()); } catch(InterruptedException e) { e.printStackTrace(); } } } } OUTPUT : - Produced - 1 Consumed - 1 Produced - 2 Consumed - 2 Produced - 3 Consumed - 3 Produced - 4 Consumed - 4 Produced - 5 Consumed - 5 Produced - 6 Consumed - 6 Produced - 7 Consumed - 7 Produced - 8 Consumed - 8 Produced - 9 Consumed - 9 Produced - 10 Consumed - 10
Related Posts :
Producer Consumer Problem - Solution using wait and notify In Java
Thread(or Multithread) interview questions & answers
while (true) , how can this loop end? i wonder, its working but how?
ReplyDeleteBlockingQueue internally it will call the break statement to end the loop if there is no any producer in the queue.
DeleteThis article is really good for all newbie here. Thank you for sharing with us! Good luck!
ReplyDeleteanjali pichai
Alaina Marie Mathers