Custom implementation of LinkedBlockingQueue class which implements BlockingQueue interface in java - Detailed explanation with full program




Contents of page :
  • 1) Key Features of own/custom BlockingQueue in java >
  • 2) Methods used >
  • 3) Example/ Program which gives custom implementation of BlockingQueue interface and LinkedBlockingQueue class in java >
  • Summary >


In this post, we will provide custom implementation of following -
  • BlockingQueue interface in java and,
  • LinkedBlockingQueue class which implements BlockingQueue interface in java.

1) Key Features of own/custom BlockingQueue in java >

  • This BlockingQueue implementation follows FIFO (first-in-first-out).
  • New elements are inserted at the tail of the queue in java and,
  • Removal elements is done at the head of the queue in java.
  • Blocking queue internally uses Linked List for implementing Queue in java.


2) Methods used in java >
void put(E item) throws InterruptedException ;  
Inserts the specified element into this queue only if space is available else waits for space to become available.
E take()  throws InterruptedException;
Retrieves and removes the head of this queue Retrieves and removes the head of this queue waits for element to become available.


3) Example/ Program which gives custom implementation of >
  • BlockingQueue interface in java and,
  • LinkedBlockingQueue class in java.

import java.util.LinkedList;
import java.util.List;
/**
* Implementing custom BlockingQueue interface .
* This BlockingQueue implementation follows FIFO (first-in-first-out).
* New elements are inserted at the tail of the queue,
* and removal elements is done at the head of the queue.
*
* @author AnkitMittal
* Copyright (c), AnkitMittal .
* All Contents are copyrighted and must not be reproduced in any form.
*/
interface BlockingQueueCustom<E> {
    /**
     * Inserts the specified element into this queue
     * only if space is available else
     * waits for space to become available.
     */
    void put(E item)  throws InterruptedException ;
    /**
     * Retrieves and removes the head of this queue
     * only if elements are available else
     * waits for element to become available.
     */
    E take()  throws InterruptedException;
}

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
/**
* Implementing custom LinkedBlockingQueue class.
* This BlockingQueue implementation follows FIFO (first-in-first-out).
* New elements are inserted at the tail of the queue,
* and removal elements is done at the head of the queue.
*
* @author AnkitMittal
* Copyright (c), AnkitMittal .
* All Contents are copyrighted and must not be reproduced in any form.
*/
class LinkedBlockingQueueCustom<E> implements BlockingQueueCustom<E>{
    private List<E> queue;
    private int  maxSize ; //maximum number of elements queue can hold at a time.
    public LinkedBlockingQueueCustom(int maxSize){
          this.maxSize = maxSize;
          queue = new LinkedList<E>();
    }
    /**
     * Inserts the specified element into this queue
     * only if space is available else
     * waits for space to become available.
     * After inserting element it notifies all waiting threads.
     */
    public synchronized void put(E item)  throws InterruptedException  {
     
         //check space is available or not.
             if (queue.size() == maxSize) {
         this.wait();
             }
            
             //space is available, insert element and notify all waiting threads.
          queue.add(item);
          this.notifyAll();
    }
    /**
     * Retrieves and removes the head of this queue
     * only if elements are available else
     * waits for element to become available.
     * After removing element it notifies all waiting threads.
     */
    public synchronized E take()  throws InterruptedException{
       //waits element is available or not.
            if (queue.size() == 0) {
                this.wait();
            }
            //element is available, remove element and notify all waiting threads.
            this.notifyAll();
      return queue.remove(0);
         
    }
}
/**
* Main class
*/
public class BlockingQueueCustomTest{
   public static void main(String[] args) throws InterruptedException {
      BlockingQueueCustom<Integer> b=new LinkedBlockingQueueCustom<Integer>(10);
      System.out.println("put(11)");
      b.put(11);
      System.out.println("put(12)");
      b.put(12);
      System.out.println("take() > "+b.take());
      System.out.println("take() > "+b.take());
   }
}
/*OUTPUT
put(11)
put(12)
take() > 11
take() > 12
*/


Summary >

put(E item) method in java -
Inserts the specified element into this queue only if space is available else waits for space to become available. After inserting element it notifies all waiting threads in java.
check space is available or not, if space is not available wait, else if space is available, insert element and notify all waiting threads in java.


E take() method in java -
       
Retrieves and removes the head of this queue only if elements are available else waits for element to become available. After removing element it notifies all waiting threads in java.
Check element is available or not, if element is not available wait, else if element is available, remove element and notify all waiting threads in java.
  
So in this thread tutorial we learned how to give
own/Custom implementation of LinkedBlockingQueue class in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>

Consumer Producer problem solution using different techniques in java >
Solve Consumer Producer problem by using wait() and notify() methods in multithreading in java

solve Consumer Producer problem by using wait() and notify() methods, where consumer can consume only when production is over


How to solve Consumer Producer problem without using wait() and notify() methods, where consumer can consume only when production is over.



BlockingQueue in java >

Solve Consumer Producer problem by using BlockingQueue in multithreading in java


Custom implementation of LinkedBlockingQueue class which implements BlockingQueue interface





Interviews in java >

THREADS - Top 80 interview questions and answers (detailed explanation with programs) Set-1 >  Q1- Q60

THREADS - Top 80 interview questions and answers, important interview OUTPUT questions and answers, Set-2 > Q61- Q80


eEdit
Must read for you :