Java LinkedBlockingQueue

LinkedBlockingQueue is an implementation of the BlockingQueue, Which is backed by the Linked Node Data Structure. LinkedBlockingQueue can be initialized with and without the specified limit.

Specified limit should be with in the limit of primitive data type int , the following are constructors of the LinkedBlockingQueue.

capacity is the limit in terms of how many elements can be inserted in LinkedBlockingQueue at max, Collection is the list of elements which needs to be inserted into the queue at the time of initialization. And queue will be accessed in the terms of FIFO no need of any other parameters like ArrayBlockingQueue.

This Collection size should be less than the capacity of the LinkedBlockingQueue otherwise this will lead to an java.lang.IllegalStateException

The following are the most commonly used methods in the LinkedBlockingQueue.

Insert Operation

Remove Operation

Examine Operation

LinkedBlockingQueue Example in Java

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class LinkedBlockingQueueExample {

	public static void main(String[] args) throws InterruptedException {
		BlockingQueue linkedblockingQueue=new LinkedBlockingQueue(4);
		linkedblockingQueue.add("LinkedBlockingQueue");
		linkedblockingQueue.add("example");
		linkedblockingQueue.add("In");
		linkedblockingQueue.add("Java");
		System.out.println(linkedblockingQueue);		
	}
}						
output
[LinkedBlockingQueue, example, In, Java]