• Register
Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Custom BlockingQueue Implementation in java

0 votes
19 views
Custom BlockingQueue Implementation in java.
asked Aug 2 in Java by Aanchal Jain

1 Answer

0 votes
BlockingQueue:- Java BlockingQueue is an interface which placed in java.util.concurrent.BlockingQueue package. BlockingQueue doesn’t accept null values and throw NullPointerException if you try to store null value in the queue. BlockingQueue implementations are thread-safe. All queuing methods are atomic in nature and use internal locks or other forms of concurrency control.

See Following example to implement Custom BlockingQueue:-
package com.ds.queue.blockingQueue;
/**
 * Implementation of custom BlockingQueue interface.
 * The BlockingQueue follows the FIFO (First-In-First-Out).
 *
 **/
interface CustomBlockingQueue<T> {
 
      /**
       * Inserts the element into blocking queue
       * if space is available otherwise waits for space to be available.
       */
      void put(T item)  throws InterruptedException ;
 
 
      /**
       * Retrieve and remove element from head of queue
       * if elements are available otherwise waits for element to be available.
       */
      T take()  throws InterruptedException;
}

package com.ds.queue.blockingQueue;

import java.util.LinkedList;
import java.util.List;

/**
 * Implementing our custom LinkedBlockingQueue class.
 * The BlockingQueue implementation follows the FIFO (First-In-First-Out).
 * New element inserts at the tail of queue,
 * Remove elements from head of the queue.
 */
public class CustomLinkedBlockingQueue<T> implements CustomBlockingQueue<T>{

    private List<T> queue;
    //maximum elements queue can hold at a time.
    private int  maxSize ;

    public CustomLinkedBlockingQueue(int maxSize){
        this.maxSize = maxSize;
        queue = new LinkedList<T>();
    }


    /**
     * Inserts the element into blocking queue
     * if space is available otherwise waits for space to be available.
     */
    public synchronized void put(T 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();
    }


    /**
     * Retrieve and remove element from head of queue
     * if elements are available otherwise waits for element to be available.
     */
    public synchronized T 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);

    }
}

package com.ds.queue.blockingQueue;

/**
 * Main test class of blocking queue
 */
public class CustomBlockingQueueTest{
    public static void main(String[] args) throws InterruptedException {
        CustomBlockingQueue<Integer> customBlockingQueue = new CustomLinkedBlockingQueue<Integer>(10);
        System.out.println("put(1)");
        customBlockingQueue.put(1);
        System.out.println("put(2)");
        customBlockingQueue.put(2);
        System.out.println("take(): "+customBlockingQueue.take());
        System.out.println("take(): "+customBlockingQueue.take());
 
    }
}

Output:-
put(1)
put(2)
take(): 1
take(): 2
answered Aug 2 by ranju_12 (1,740 points)
...