Showing posts with label ArrayBlockingQueue. Show all posts
Showing posts with label ArrayBlockingQueue. Show all posts

Monday, July 1, 2019

Producer Consumer Example Using BlockingQueue in Java

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.techbyteslearn.lab.concurrent; import java.util.concurrent.BlockingQueue; public class FoodProducer implements Runnable { private BlockingQueue<String> producerQueue = null; public FoodProducer(BlockingQueue<String> queue) { producerQueue = queue; } @Override 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) { Thread.currentThread().interrupt(); e.printStackTrace(); } } }

 

FoodConsumer.java

 

package com.techbyteslearn.lab.concurrent;

import java.util.concurrent.BlockingQueue;

public class FoodConsumer implements Runnable {

    private BlockingQueue<String> consumerQueue = null;

    public FoodConsumer(BlockingQueue<String> consumerQueue) {
        this.consumerQueue = consumerQueue;
    }

    @Override
    public void run() {
        try {
            System.out.println(consumerQueue.take());
            System.out.println(consumerQueue.take());
            System.out.println(consumerQueue.take());

            Thread.sleep(2000);

        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
    }
}

 

MainFoodProcess.java

 

package com.techbyteslearn.lab.concurrent; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class MainFoodProcess { public static void main(String[] args) throws InterruptedException { final BlockingQueue<String> queue = new ArrayBlockingQueue<>(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 important point in this example is that the ArrayBlockingQueue has a capacity of 2, while the producer adds three items. The put() method blocks when the queue is full until the consumer takes an item from the queue.

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.