dsa

Java LinkedBlockingQueue

In this tutorial, we will learn about the LinkedBlockingQueue, its contructors and methods and how to implement it in Java.

Java Collection frameworks provide LinkedBlockingQueue class and it implements BlockingQueue Interface.

The LinkedBlockingQueue Class in Java has a blocking queue that is optionally bounded and based on linked nodes. This means that if the capacity is provided then the LinkedBlockingQueue is bound, otherwise it is not bound.

Constructors of the LinkedBlockingQueue

ConstructorsDescription
LinkedBlockingQueue() Creates a LinkedBlockingQueue with the default initial capacity (11).
LinkedBlockingQueue(Collection c) Creates a LinkedBlockingQueue containing the elements in the specified collection.
LinkedBlockingQueue(int initialCapacity) Creates a LinkedBlockingQueue with the specified initial capacity.

Methods of LinkedBlockingQueue

Apart from the methods inherited from its parent class/interface ,it has following methods :

MethodsDescription
void clear()Atomically removes all of the elements from this queue.
boolean contains(Object o)Returns true if this queue contains the specified element.
int drainTo(Collection c)Removes all available elements from this queue and adds them to the given collection.
int drainTo(Collection c, int maxElements)Removes at most the given number of available elements from this queue and adds them to the given collection.
Iterator iterator()Returns an iterator over the elements in this queue in proper sequence.
boolean offer(E e)Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue’s capacity, returning true upon success and false if this queue is full.
boolean offer(E e, long timeout, TimeUnit unit)Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available.
E peek()Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
E poll()Retrieves and removes the head of this queue, or returns null if this queue is empty.
E poll(long timeout, TimeUnit unit)Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
void put(E e)Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.
int remainingCapacity()Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking.
boolean remove(Object o)Removes a single instance of the specified element from this queue, if it is present.
int size()Returns the number of elements in this queue.
Spliterator spliterator()Returns a Spliterator over the elements in this queue.
E take()Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
Object[] toArray()Returns an array containing all of the elements in this queue, in proper sequence.
T[] toArray(T[] a)Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array.
String toString()Returns a string representation of this collection.

Example


import java.util.concurrent.LinkedBlockingQueue;
import java.util.Iterator;
class CodeMistic 
{
    	public static void main(String[] args) 
	{
        	LinkedBlockingQueue city = new LinkedBlockingQueue<>(5);

        	// Using add()
        	city.add("Pune");
        	city.add("Bhopal");
		city.add("Hazaribagh");
        	// Using offer()
        	city.offer("Patna");
        	System.out.println("LinkedBlockingQueue: " + city);

		// Using peek()
        	String city1 = city.peek();
        	System.out.println("Accessed Element: " + city1);

        	// Using iterator()
        	Iterator iterate = city.iterator();
        	System.out.print("LinkedBlockingQueue Elements: ");

        	while(iterate.hasNext()) 
		{
            		System.out.print(iterate.next()+" ");
        	}
		System.out.println("");

		// Using remove()
        	String city2 = city.remove();
        	System.out.println("Removed Element:");
        	System.out.println("Using remove(): " + city2);

        	// Using poll()
        	String city3 = city.poll();
        	System.out.println("Using poll(): " + city3);

        	// Using clear()
        	city.clear();
        	System.out.println("Updated LinkedBlockingQueue " + city);
    	}
}


Output:

LinkedBlockingQueue: [Pune, Bhopal, Hazaribagh, Patna]
Accessed Element: Pune
LinkedBlockingQueue Elements: Pune Bhopal Hazaribagh Patna 
Removed Element:
Using remove(): Pune
Using poll(): Bhopal
Updated LinkedBlockingQueue []