01 July, 2019

Producer-Consumer problem solution using java - BlockingQueue

Threading is a very tricky and interesting concept in java programming language. There are many problems we face in technology out of which producer-consumer is one. Today we will write a java program for showing producer consumer problem and its solution by using BlockingQueue implementation. 

In this program we will use ArrayBlockingQueue

FoodProducer.java
package com.javadevelopersguide.lab.concurrent;
import java.util.concurrent.BlockingQueue;
/**
 *
 * @author manoj.bardhan
 *
 */
public class FoodProducer implements Runnable {
private BlockingQueue<String> producerQueue = null;
public FoodProducer(BlockingQueue<String> queue) {
producerQueue = queue;
}
public void run() {
try {
producerQueue.put("Drinks");
Thread.sleep(2000);
producerQueue.put("Chocolates");
Thread.sleep(2000);
producerQueue.put("Fruits");
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


FoodConsumer.java
package com.javadevelopersguide.lab.concurrent;
import java.util.concurrent.BlockingQueue;
/**
 * @author manoj.bardhan
 *
 */
public class FoodConsumer implements Runnable {
private BlockingQueue<String> consumerQueue = null;
public FoodConsumer(BlockingQueue<String> consumerQueue) {
this.consumerQueue = consumerQueue;
}
public void run() {
try {
System.out.println(consumerQueue.take());
System.out.println(consumerQueue.take());
System.out.println(consumerQueue.take());
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}


Now we will have a main class to call and execute these two workers. 

MainFoodProcess.java

package com.javadevelopersguide.lab.concurrent;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
 *
 * @author manoj.bardhan
 *
 */
public class MainFoodProcess {
public static void main(String[] args) throws InterruptedException {
final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(2);
FoodProducer producer = new FoodProducer(queue);
FoodConsumer consumer = new FoodConsumer(queue);
new Thread(producer).start();
new Thread(consumer).start();
Thread.sleep(3000);
}
}


Output :

Drinks 
Chocolates 
Fruits


The output here is that, every time the producer insert element into the Queue the consumer will take that element out of the queue. 

Here we have used the below 2 important methods take() and put(). There are few many method provided by the BlockingQueue implementation. Find more methods on BlockingQueue.

take() - Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
put() - Inserts the specified element into this queue, waiting if necessary for space to become available.


Happy Learning.






4 comments:

  1. The reason to help from the comment to help you get easy and simple techniques to get your current location as well as manually entered address's zip code, postal code or PIN Code with the help of Zip Code Lookup.

    ReplyDelete
  2. Good Post! Thank you so much for sharing the post, it was so good to read and useful to improve
    Java Training in Electronic City

    ReplyDelete
  3. Very nice information on java. You can also check goformule.com for mulesoft tutorials

    ReplyDelete
  4. Thanks for sharing such great information, I highly appreciate your hard-working skills as the post you published have some great information which is quite beneficial for me, I hope you will post more like that in the future.
    CPS test

    ReplyDelete