0
Sponsored Links


Ad by Google
In this tutorial, I am trying to explain you, What is LinkedBlockingQueue? How to use LinkedBlockingQueue? and solve very popular producer consumer problem with the help of LinkedBlockingQueue.

What is LinkedBlockingQueue ?

The LinkedBlockingQueue is an implementation of BlockingQueue interface, which extends Queue and Queue extends Collection interface.
That means LinkedBlockingQueue act as Queue(FIFO) and supports all the operations of Collection interface.
The LinkedBlockingQueue, is an optionally bounded(optional to specify the capacity of the queue, if not specify, than the default capacity would be Integer.MAX_VALUE ) blocking queue based on linked nodes, unlike ArrayBlockingQueue. LinkedBlockingQueue will throws out of memory exception once it's capacity increased and trying to insert element into the queue.
The LinkedBlockingQueue works in FIFO(first-in-first-out) data structure, that means, the first element inserted into the queue will be retrieved first from the queue.
The LinkedBlockingQueue is an implementation of BlockingQueue, so it's thread safe and designed to be used primarily for producer-consumer queues.

How to use LinkedBlockingQueue

The LinkedBlockingQueue has three different constructor to create LinkedBlockingQueue
  • LinkedBlockingQueue() - default capacity is Integer.MAX_VALUE
  • LinkedBlockingQueue(Collection<? extends E> c)
  • LinkedBlockingQueue(int capacity)
All the methods of LinkedBlockingQueue act same as ArrayBlockingQueue because it's also an implementation of BlockigQueue, only the internal implementations are different, please read about BlockingQueue before implementing LinkedBlockingQueue.

Let's see an example of LinkedBlockingQueue.
Chef.java class which will produce the pizza
package com.javamakeuse.poc;

import java.util.concurrent.BlockingQueue;

/**
 * Chef class to produce pizza
 * 
 * @author subodh.ray
 * 
 */
public class Chef implements Runnable {

 private final BlockingQueue<String> bQueue;
 private final String message;
 public Chef(BlockingQueue<String> bQueue, String message) {
  this.bQueue = bQueue;
  this.message = message;
 }

 @Override
 public void run() {
  bQueue.add(message);
 }

}
Customer.java class to consume the pizza produced by producer(Chef.java)
package com.javamakeuse.poc;

import java.util.concurrent.BlockingQueue;

/**
 * Customer class to consumed pizza
 * 
 * @author subodh.ray
 * 
 */
public class Customer implements Runnable {

 private BlockingQueue<String> bQueue;

 public Customer(BlockingQueue<String> bQueue) {
  this.bQueue = bQueue;
 }

 @Override
 public void run() {
  try {
   for (String message = bQueue.take(); message != null; message = bQueue
     .take()) {
    System.out.println("Consumed - " + message);
   }
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }

}
Restaurant.java class where pizza produced and consumed.
package com.javamakeuse.poc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
 * Restaurant class where pizza produce and consumed
 * 
 * @author subodh.ray
 * 
 */
public class Restaurant {

 public static void main(String[] args){

  BlockingQueue<String> bQueue = new LinkedBlockingQueue<String>();

  // producing in queue
  (new Thread(new Chef(bQueue, "Large Pizza"))).start();
  (new Thread(new Chef(bQueue, "Medium Pizza"))).start();

  // adding 3rd element will throws IllegalStateException
  // because queue is already full.
  (new Thread(new Chef(bQueue, "Small Pizza"))).start();

  // Consuming from the queue
  (new Thread(new Customer(bQueue))).start();
 }

}
OUT PUT:
Consumed - Large Pizza
Consumed - Medium Pizza
Consumed - Small Pizza

Done :)
Sponsored Links

0 comments:

Post a Comment