ConcurrentLinkedQueue in Java
Last Updated : 12 Feb, 2025
In Java, the ConcurrentLinkedQueue is the part of the java.util.concurrent package and implements a FIFO(First-In-First-Out) queue. It is a thread-safe, non-blocking, and scalable queue designed for use in highly concurrent environments. The queue uses a lock-free algorithm, ensuring that multiple threads can safely enqueue and dequeue elements without the need for external synchronization.
- The queue allows multiple threads to perform operations without locking
- It can be accessed by multiple threads concurrently without causing data corruption
- The queue maintains the order of elements as they are added
Example: This example demonstrates creating a thread-safe ConcurrentLinkedQueue, adding elements to it and printing the queue.
Java // Java Program to demonstrate the working of // ConcurrentLinkedQueue import java.util.concurrent.ConcurrentLinkedQueue; public class Geeks { public static void main(String[] args) { // Create a ConcurrentLinkedQueue ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>(); // Adding elements to the queue q.offer(1); q.offer(2); q.offer(3); System.out.println("Queue after adding elements: " + q); } }
OutputQueue after adding elements: [1, 2, 3]
Hierarchy of ConcurrentLinkedQueue
java.lang.Object
↳ java.util.AbstractCollection<E>
↳ java.util.AbstractQueue<E>
↳ Class ConcurrentLinkedQueue<E>
Declaration of ConcurrentLinkedQueue
In Java, the declaration of ConcurrentLinkedQueue can be done as:
ConcurrentLinkedQueue<Type> queueName = new ConcurrentLinkedQueue<>();
Constructors
Constructor | Description |
---|
ConcurrentLinkedQueue() | It is used to construct an empty queue. |
ConcurrentLinkedQueue(Collection<E> c) | It is used to construct a queue with the elements of the Collection passed as the parameter. |
Example 1: This example demonstrates creating and initializing a ConcurrentLinkedQueue both with individual elements and by copying another queue using a constructor.
Java // Java Program demonstrating both with individual elements // and by copying another queue using a constructor import java.util.concurrent.*; class Geeks { public static void main(String[] args) { // Create a ConcurrentLinkedQueue // using ConcurrentLinkedQueue() constructor ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>(); q.add(10); q.add(20); q.add(30); q.add(40); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + q); // Create a ConcurrentLinkedQueue // using ConcurrentLinkedQueue(Collection c) // constructor ConcurrentLinkedQueue<Integer> q1 = new ConcurrentLinkedQueue<Integer>(q); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue1: " + q1); } }
OutputConcurrentLinkedQueue: [10, 20, 30, 40] ConcurrentLinkedQueue1: [10, 20, 30, 40]
Example 2: This example demonstrates the use of ConcurrentLinkedQueue methods like peek(), poll() and size() to interact with and manipulate the queue.
Java // Java Program to demosntrates the // working of peek(), poll(), size() import java.util.concurrent.*; class Geeks { public static void main(String[] args) { // Create a ConcurrentLinkedQueue // using ConcurrentLinkedQueue() // constructor ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>(); q.add(10); q.add(20); q.add(30); q.add(40); System.out.println("ConcurrentLinkedQueue: " + q); // Displaying the first element // using peek() method System.out.println("First Element is: " + q.peek()); // Remove and display the first element // using poll() method System.out.println("Head Element is: " + q.poll()); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + q); // Get the size using size() method System.out.println("Size: " + q.size()); } }
OutputConcurrentLinkedQueue: [10, 20, 30, 40] First Element is: 10 Head Element is: 10 ConcurrentLinkedQueue: [20, 30, 40] Size: 3
Performing Various Operations on ConcurrentLinkedQueue
1. Adding Elements: We use add() and addAll() method to insert elements in a ConcurrentLinkedQueue.
Example: This example demonstrates how to add individual elements and a collection of elements to a ConcurrentLinkedQueue using add() and addAll().
Java // Java Program to demonstrate adding // elements to ConcurrentLinkedQueue // using add() and addAll() method import java.util.*; import java.util.concurrent.*; public class Geeks { public static void main(String[] args) { // Create an instance of ConcurrentLinkedQueue ConcurrentLinkedQueue<String> q = new ConcurrentLinkedQueue<String>(); // Add String to queue using add method q.add("Java"); q.add("C++"); q.add("Python"); q.add("Js"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + q); // create a ArrayList of Strings ArrayList<String> al = new ArrayList<String>(); // add String to ArrayList al.add("Geek1"); al.add("Geek2"); al.add("Geek3"); al.add("Geek4"); al.add("Geek5"); // Displaying the existing Collection System.out.println("Collection to be added: " + al); // apply addAll() method and passed // the arraylist as parameter boolean b = q.addAll(al); // Displaying the existing ConcurrentLinkedQueue System.out.println("Collection added: " + b); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + q); } }
Output:
2. Removing Elements: We can use remove() to remove elements from the ConcurrentLinkedQueue.
Example: This example demonstrates how to remove a specific element from a ConcurrentLinkedQueue using the remove() method.
Java // Java Program to Demonstrate the // working of remove() method import java.util.concurrent.*; public class Geeks { public static void main(String[] args) { // Create an instance of ConcurrentLinkedQueue ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>(); // Add Numbers to queue using add(e) method q.add(10); q.add(20); q.add(30); q.add(40); System.out.println("ConcurrentLinkedQueue: " + q); // apply remove() for Number 78249 boolean b = q.remove(20); System.out.println( "number 20 remove successfully? : " + b); // Displaying the existing ConcurrentLinkedQueue System.out.println("Updated ConcurrentLinkedQueue: " + q); } }
OutputConcurrentLinkedQueue: [10, 20, 30, 40] number 20 remove successfully? : true Updated ConcurrentLinkedQueue: [10, 30, 40]
3. Iterating Elements: We can use the iterator() method of ConcurrentLinkedQueue which return an iterator that allows traversing through the elements of the queue in the FIFO order.
Example: This example demonstrates how to use the iterator() method of ConcurrentLinkedQueue to traverse and print its elements in FIFO order.
Java // Java Program Demonstrate the // working of iterator() method import java.util.*; import java.util.concurrent.*; public class Geeks { public static void main(String[] args) { // Create an instance of ConcurrentLinkedQueue ConcurrentLinkedQueue<String> q = new ConcurrentLinkedQueue<String>(); // Add String to queue using add(e) method q.add("Java"); q.add("C++"); q.add("Python"); q.add("js"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + q); // Call iterator() method Iterator i = q.iterator(); // Print elements of iterator System.out.println( "The String Values of iterator are:"); while (i.hasNext()) { System.out.print(i.next() + " "); } } }
OutputConcurrentLinkedQueue: [Java, C++, Python, js] The String Values of iterator are: Java C++ Python js
4. Accessing Elements: We can use peek() and element() to access the elements of ConcurrentLinkedQueue.
Example: This example demonstrates how to access the head element of a ConcurrentLinkedQueue using element() and peek() method.
Java // Java Program Demonstrate the // working of element() and peek() import java.util.concurrent.*; public class Geeks { public static void main(String[] args) throws IllegalStateException { // Create an instance of ConcurrentLinkedQueue ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>(); // Add numbers to end of Queue q.add(10); q.add(20); q.add(30); q.add(40); System.out.println("Queue: " + q); // print head using element() System.out.println("Queue's head: " + q.element()); // print head using peek() System.out.println("Queue's head: " + q.peek()); } }
OutputQueue: [10, 20, 30, 40] Queue's head: 10 Queue's head: 10
Methods of ConcurrentLinkedQueue
Method | Description |
---|
add(E e) | Inserts the specified element at the tail of this queue. |
addAll(Collection<? extends E> c) | Appends all of the elements in the specified collection to the end of this queue, in the order that they are returned by the specified collection's iterator. |
contains(Object o) | Returns true if this queue contains the specified element. |
forEach(Consumer<? super E> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
isEmpty() | Returns true if this queue contains no elements. |
iterator() | Returns an iterator over the elements in this queue in the proper sequence. |
offer(E e) | Inserts the specified element at the tail of this queue. |
remove(Object o) | Removes a single instance of the specified element from this queue, if it is present. |
removeAll(Collection<?> c) | Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
removeIf(Predicate<? super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
retainAll(Collection<?> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
size() | Returns the number of elements in this queue. |
spliterator() | Returns a Spliterator over the elements in this queue. |
toArray() | Returns an array containing all of the elements in this queue, in the proper sequence. |
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. |
Methods Declared in Class java.util.AbstractQueue
Methods | Description |
---|
clear() | Removes all the elements from this queue. |
element() | Retrieves, but does not remove, the head of this queue. |
remove() | Retrieves and removes the head of this queue. |
Methods Declared in Class java.util.AbstractCollection
Method | Description |
---|
containsAll(Collection<?> c) | Returns true if this collection contains all of the elements in the specified collection. |
toString() | Returns a string representation of this collection. |
Methods Declared in Interface java.util.Collection
Method | Description |
---|
clear() | Removes all of the elements from this collection (optional operation). |
containsAll(Collection<?> c) | Returns true if this collection contains all of the elements in the specified collection. |
equals(Object o) | Compares the specified object with this collection for equality. |
hashCode() | Returns the hash code value for this collection. |
parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
stream() | Returns a sequential Stream with this collection as its source. |
toArray(IntFunction<T[]> generator) | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
Methods Declared in Interface java.util.Queue
Method | Description |
---|
element() | Retrieves, but does not remove, the head of this queue. |
peek() | Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. |
poll() | Retrieves and removes the head of this queue, or returns null if this queue is empty. |
remove() | Retrieves and removes the head of this queue. |