GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — ConcurrentLinkedQueue in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

ConcurrentLinkedQueue in Java

Last Updated : 4 Nov, 2026

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
import java.util.concurrent.ConcurrentLinkedQueue;

public class Geeks {
    public static void main(String[] args){

        ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>();

        q.offer(1);
        q.offer(2);
        q.offer(3);

        System.out.println("Queue after adding elements: " + q);
    }
}

Output
Queue after adding elements: [1, 2, 3]

Hierarchy of ConcurrentLinkedQueue

ConcurrentLinkedQueue extends AbstractQueue and implements Queue, Iterable, Collection, and Serializable interfaces.

ConcurrentLinkedQueue-in-Java-with-Examples
Hierarchy of ConcurrentLinkedQueue

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: Creating and initializing a ConcurrentLinkedQueue both with individual elements and by copying another queue using a constructor.

Java
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);
    }
}

Output
ConcurrentLinkedQueue: [10, 20, 30, 40]
ConcurrentLinkedQueue1: [10, 20, 30, 40]

Example 2: Use of ConcurrentLinkedQueue methods like peek(), poll() and size() to interact with and manipulate the queue.

Java
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());
    }
}

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
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 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:

output
Output

2. Removing Elements

We can use remove() to remove elements from the ConcurrentLinkedQueue.

Java
import java.util.concurrent.*;

public class Geeks {

    public static void main(String[] args){
        // Create an instance of ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>();

        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);
    }
}

Output
ConcurrentLinkedQueue: [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.

Java
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>();

        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() + " ");
        }
    }
}

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
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());
    }
}

Output
Queue: [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.


Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
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.