The LinkedBlockingQueue is an optionally-bounded blocking queue based on linked nodes. It means that the LinkedBlockingQueue can be bounded, if its capacity is given, else the LinkedBlockingQueue will be unbounded. The capacity can be given as a parameter to the constructor of LinkedBlockingQueue. This queue orders elements FIFO (first-in-first-out). It means that the head of this queue is the oldest element of the elements present in this queue. The tail of this queue is the newest element of the elements of this queue. The newly inserted elements are always inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.
The capacity, if unspecified, is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion, till the capacity of the queue is not filled. This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. It is a member of the Java Collections Framework.
In order to create a linked blocking queue, we must import the java.util.concurrent.LinkedBlockingQueue package.
// Creating String type ArrayBlockingQueue with size 5
LinkedBlockingQueue<String> animal = new LinkedBlockingQueue<>();
// Creating Integer type ArrayBlockingQueue with size 5
LinkedBlockingQueue<String> animal = new LinkedBlockingQueue<>(int capacity);
The LinkedBlockingQueue uses linked lists as its internal storage. It is considered as a thread-safe collection. Hence, it is generally used in multi-threading applications. Suppose, one thread is inserting elements to the queue and another thread is removing elements from the queue. Now, if the first thread is slower than the second thread, then the linked blocking queue can make the second thread waits until the first thread completes its operations.
In order to create a LinkedBlockingQueue, we need to create an object of the LinkedBlockingQueue class. The LinkedBlockingQueue class consists of various constructors that allow the possible creation of the LinkedBlockingQueue. The following are the constructors available in this class.
Constructor | Description | Example |
---|---|---|
LinkedBlockingQueue() | Creates a LinkedBlockingQueue using default constructor. | LinkedBlockingQueue lbq = new LinkedBlockingQueue(); |
LinkedBlockingQueue(int capacity) | Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE. | LinkedBlockingQueue lbq = new LinkedBlockingQueue(int capacity); |
The ArrayBlockingQueue class provides the implementation of all the methods in the BlockingQueue interface.
Method Name | Description |
---|---|
add() | Inserts the specified element to the linked blocking queue. It throws an exception if the queue is full. |
offer() | Inserts the specified element to the linked blocking queue. It returns false if the queue is full. |
peek() | Returns an element from the front of the linked blocking queue. It returns null if the queue is empty. |
remove() | Returns and removes a specified element from the linked blocking queue. It throws an exception if the queue is empty. |
poll() | Returns and removes a specified element from the linked blocking queue. It returns null if the queue is empty. |
clear() | Removes all the elements from the linked blocking queue. |
put() | To insert the specified element to the end of a linked blocking queue, we use the put() method. |
take() | To return and remove an element from the front of the linked blocking queue, we can use the take() method. |
contains(element) | Searches the linked blocking queue for the specified element. If the element is found, it returns true, if not it returns false. |
size() | Returns the length of the linked blocking queue. |
toArray() | Converts linked blocking queue to an array and return the array. |
toString() | Converts the linked blocking queue to string. |
The LinkedBlockingQueue class provides various methods to perform different operations on LinkedBlockingQueue. We will look at some commonly used LinkedBlockingQueue operations.
import java.util.concurrent.LinkedBlockingQueue;
class Main {
public static void main(String[] args) {
LinkedBlockingQueue<String> animals = new LinkedBlockingQueue<>(5);
// Using add()
animals.add("Dog");
animals.add("Cat");
// Using offer()
animals.offer("Horse");
System.out.println("LinkedBlockingQueue: " + animals);
}
}
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Iterator;
class Main {
public static void main(String[] args) {
LinkedBlockingQueue<String> animals = new LinkedBlockingQueue<>(5);
// Add elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("LinkedBlockingQueue: " + animals);
// Using peek()
String element = animals.peek();
System.out.println("Accessed Element: " + element);
// Using iterator()
Iterator<String> iterate = animals.iterator();
System.out.print("LinkedBlockingQueue Elements: ");
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}
// for each loop
for(String str: animals) {
System.out.println(str);
}
}
}
import java.util.concurrent.LinkedBlockingQueue;
class Main {
public static void main(String[] args) {
LinkedBlockingQueue<String> animals = new LinkedBlockingQueue<>(5);
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("LinkedBlockingQueue: " + animals);
// Using remove()
String element1 = animals.remove();
System.out.println("Removed Element:");
System.out.println("Using remove(): " + element1);
// Using poll()
String element2 = animals.poll();
System.out.println("Using poll(): " + element2);
// Using clear()
animals.clear();
System.out.println("Updated LinkedBlockingQueue: " + animals);
}
}
import java.util.concurrent.LinkedBlockingQueue;
class Main {
public static void main(String[] args) {
LinkedBlockingQueue<String> animals = new LinkedBlockingQueue<>(5);
try {
// Add elements to animals
animals.put("Dog");
animals.put("Cat");
System.out.println("LinkedBlockingQueue: " + animals);
}
catch(Exception e) {
System.out.println(e);
}
}
try {
//Add elements to animals
animals.put("Dog");
animals.put("Cat");
System.out.println("LinkedBlockingQueue: " + animals);
// Remove an element
String element = animals.take();
System.out.println("Removed Element: " + element);
}
catch(Exception e) {
System.out.println(e);
}
}