ConcurrentLinkedQueue is a thread-safe, non-blocking queue in Java designed for high-performance concurrent applications. It follows the FIFO principle and uses a lock-free mechanism for efficient multi-threading.
Uses a lock-free (non-blocking) algorithm, improving performance in highly concurrent environments.
Does not block threads—methods like offer(), poll(), and peek() return immediately.
Provides weakly consistent iterators that do not throw ConcurrentModificationException.
Java
importjava.util.concurrent.ConcurrentLinkedQueue;publicclassGeeks{publicstaticvoidmain(String[]args){ConcurrentLinkedQueue<Integer>q=newConcurrentLinkedQueue<>();q.offer(1);q.offer(2);q.offer(3);System.out.println("Queue after adding elements: "+q);}}
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: Creating and initializing a ConcurrentLinkedQueue both with individual elements and by copying another queue using a constructor.
Java
importjava.util.concurrent.*;classGeeks{publicstaticvoidmain(String[]args){// Create a ConcurrentLinkedQueue using ConcurrentLinkedQueue() constructorConcurrentLinkedQueue<Integer>q=newConcurrentLinkedQueue<Integer>();q.add(10);q.add(20);q.add(30);q.add(40);// Displaying the existing ConcurrentLinkedQueueSystem.out.println("ConcurrentLinkedQueue: "+q);// Create a ConcurrentLinkedQueue using ConcurrentLinkedQueue(Collection c) constructorConcurrentLinkedQueue<Integer>q1=newConcurrentLinkedQueue<Integer>(q);// Displaying the existing ConcurrentLinkedQueueSystem.out.println("ConcurrentLinkedQueue1: "+q1);}}
Example 2: Use of ConcurrentLinkedQueue methods like peek(), poll() and size() to interact with and manipulate the queue.
Java
importjava.util.concurrent.*;classGeeks{publicstaticvoidmain(String[]args){// Create a ConcurrentLinkedQueue using ConcurrentLinkedQueue() constructorConcurrentLinkedQueue<Integer>q=newConcurrentLinkedQueue<Integer>();q.add(10);q.add(20);q.add(30);q.add(40);System.out.println("ConcurrentLinkedQueue: "+q);// Displaying the first element using peek() methodSystem.out.println("First Element is: "+q.peek());// Remove and display the first element using poll() methodSystem.out.println("Head Element is: "+q.poll());// Displaying the existing ConcurrentLinkedQueueSystem.out.println("ConcurrentLinkedQueue: "+q);// Get the size using size() methodSystem.out.println("Size: "+q.size());}}
Output
ConcurrentLinkedQueue: [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.
Java
importjava.util.*;importjava.util.concurrent.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Create an instance of ConcurrentLinkedQueueConcurrentLinkedQueue<String>q=newConcurrentLinkedQueue<String>();// Add String to queue using add methodq.add("Java");q.add("C++");q.add("Python");q.add("Js");// Displaying the existing ConcurrentLinkedQueueSystem.out.println("ConcurrentLinkedQueue: "+q);// create a ArrayList of StringsArrayList<String>al=newArrayList<String>();// add String to ArrayListal.add("Geek1");al.add("Geek2");al.add("Geek3");al.add("Geek4");al.add("Geek5");// Displaying the existing CollectionSystem.out.println("Collection to be added: "+al);// apply addAll() method and passed arraylist as parameterbooleanb=q.addAll(al);// Displaying the existing ConcurrentLinkedQueueSystem.out.println("Collection added: "+b);// Displaying the existing ConcurrentLinkedQueueSystem.out.println("ConcurrentLinkedQueue: "+q);}}
Output:
Output
2. Removing Elements
We can use remove() to remove elements from the ConcurrentLinkedQueue.
Java
importjava.util.concurrent.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Create an instance of ConcurrentLinkedQueueConcurrentLinkedQueue<Integer>q=newConcurrentLinkedQueue<Integer>();q.add(10);q.add(20);q.add(30);q.add(40);System.out.println("ConcurrentLinkedQueue: "+q);// apply remove() for Number 78249booleanb=q.remove(20);System.out.println("number 20 remove successfully? : "+b);// Displaying the existing ConcurrentLinkedQueueSystem.out.println("Updated ConcurrentLinkedQueue: "+q);}}
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.
Java
importjava.util.*;importjava.util.concurrent.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Create an instance of ConcurrentLinkedQueueConcurrentLinkedQueue<String>q=newConcurrentLinkedQueue<String>();q.add("Java");q.add("C++");q.add("Python");q.add("js");// Displaying the existing ConcurrentLinkedQueueSystem.out.println("ConcurrentLinkedQueue: "+q);// Call iterator() methodIteratori=q.iterator();// Print elements of iteratorSystem.out.println("The String Values of iterator are:");while(i.hasNext()){System.out.print(i.next()+" ");}}}
Output
ConcurrentLinkedQueue: [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.
Java
importjava.util.concurrent.*;publicclassGeeks{publicstaticvoidmain(String[]args)throwsIllegalStateException{// Create an instance of ConcurrentLinkedQueueConcurrentLinkedQueue<Integer>q=newConcurrentLinkedQueue<>();// Add numbers to end of Queueq.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());}}
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.
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.