Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
ConcurrentLinkedQueue in Java
Next article icon

ConcurrentLinkedQueue in Java

Last Updated : 12 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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

Hierarchy of ConcurrentLinkedQueue

java.lang.Object
↳ java.util.AbstractCollection<E>
↳ java.util.AbstractQueue<E>
↳ Class ConcurrentLinkedQueue<E>

ConcurrentLinkedQueue-in-Java-with-Examples

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

Output
ConcurrentLinkedQueue: [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());     } } 

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.

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:

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

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.

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

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.

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

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.



Next Article
ConcurrentLinkedQueue in Java

R

RishabhPrabhu
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • Java-ConcurrentLinkedQueue
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    ConcurrentLinkedDeque in Java
    The ConcurrentLinkedDeque class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. It belongs to java.util.concurrent package. It is used to implement Deque with the help of LinkedList concurrently.Iterators and spliterators a
    10 min read
    ConcurrentLinkedQueue add() method in Java
    The add() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter to add() of ConcurrentLinkedQueue, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method will never throw IllegalS
    2 min read
    ConcurrentLinkedQueue poll() method in Java
    The poll() method of ConcurrentLinkedQueue is used to remove and return the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method will return null. Syntax: public E poll() Returns: This method remove and returns the head of this ConcurrentLinkedQueue or null if t
    2 min read
    ConcurrentLinkedQueue size() method in Java
    The size() method of ConcurrentLinkedQueue is used to returns number of elements that this ConcurrentLinkedQueue contains. Syntax: public int size() Returns: This method number of elements in this ConcurrentLinkedQueue. Below programs illustrate size() method of ConcurrentLinkedQueue: Example 1: Jav
    2 min read
    ConcurrentLinkedQueue peek() method in Java
    The peek() method of ConcurrentLinkedQueue is used to return the head of the ConcurrentLinkedQueue. It retrieves but does not remove, the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method returns null. Syntax: public E peek() Returns: This method returns the
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences