What is BlockingQueue

What is BlockingQueue

Answer: BlockingQueue in an interface which extends Queue provide supports operations for queue where producer keep adding item and consumer keep consuming it. We can initialize queue with initial capacity producer will keep adding items until it capacity is full then wait for space to become available again. Consumer will consume the items if it’s available and if item not available then it will wait for next item.

Please see below time scheduler producer and consumer example with BlockingQueue where producer thread will produce Vegi Pizza after every 10 seconds and consumer will keep consuming it.

Create Pizza class:

package com.javahonk.blockingqueue;

public class Pizza {

	String pizzaName;
	String doughType;
	String sause;
	String cheezeQuantity;
	String toppings;
	public String getPizzaName() {
		return pizzaName;
	}
	public void setPizzaName(String pizzaName) {
		this.pizzaName = pizzaName;
	}
	public String getDoughType() {
		return doughType;
	}
	public void setDoughType(String doughType) {
		this.doughType = doughType;
	}
	public String getSause() {
		return sause;
	}
	public void setSause(String sause) {
		this.sause = sause;
	}
	public String getCheezeQuantity() {
		return cheezeQuantity;
	}
	public void setCheezeQuantity(String cheezeQuantity) {
		this.cheezeQuantity = cheezeQuantity;
	}
	public String getToppings() {
		return toppings;
	}
	public void setToppings(String toppings) {
		this.toppings = toppings;
	}

}

 

Create Producer class to create pizza in BlockingQueue:

package com.javahonk.blockingqueue;

import java.util.concurrent.BlockingQueue;

class Producer implements Runnable {

	private final BlockingQueue<Pizza> queue;

	Producer(BlockingQueue<Pizza> q) {
		this.queue = q;

	}

	public void run() {
		try {			
			queue.put(produce());			

		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}
	}

	Pizza produce() {

		Pizza pizza = new Pizza();
		pizza.setDoughType("Thin");
		pizza.setPizzaName("Vegi Pizza");
		pizza.setCheezeQuantity("Normal");
		pizza.setSause("Pizza Sause");
		pizza.setToppings("Green Peeper, Mushroom");

		return pizza;

	}

}

 

Create Consumer class to consume pizza from BlockingQueue:

package com.javahonk.blockingqueue;

import java.util.concurrent.BlockingQueue;

class Consumer implements Runnable {
	private final BlockingQueue<Pizza> queue;

	Consumer(BlockingQueue<Pizza> q) {
		queue = q;
	}

	public void run() {
		try {
			consume(queue.take());
		} catch (InterruptedException ex) {

		}
	}

	void consume(Pizza pizza) {
		System.out.println("\nYour pizza is ready "
				+ "with below order:");		
		System.out.println("Pizza Name: "
				+pizza.getPizzaName());
		System.out.println("Dough Type: "
				+pizza.getDoughType());
		System.out.println("Sause: "
				+pizza.getSause());
		System.out.println("Cheeze: "
				+pizza.getCheezeQuantity());
		System.out.println("Toppigs: "
				+pizza.getToppings());
	}
}

 

Finally create BlockingQueueTest class which will create BlockingQueue and start producer and consumer thread and Timer which will run every 10 seconds to produce pizza:

package com.javahonk.blockingqueue;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockingQueueTest {

	private static int i = 0;

	public static void main(String[] args) 
			throws InterruptedException {

	final BlockingQueue<Pizza> q = 
			new ArrayBlockingQueue<Pizza>(5);
	Timer timer = new Timer();

	timer.scheduleAtFixedRate(new TimerTask() {
	public void run() {

	System.out.println("\nPizza order number:" + i++);
	Producer cheezePizza = new Producer(q);
	new Thread(cheezePizza).start();
	Consumer c1 = new Consumer(q);
	new Thread(c1).start();

	}
	}, 1000, 10000);
	}

}

 

Output:

What is BlockingQueue

 

Leave a Reply

Your email address will not be published. Required fields are marked *