ConcurrentLinkedQueue in Java

Java 8Object Oriented ProgrammingProgramming

The ConcurrentLinkedQueue class in Java is used to implement a queue using a concurrent linked list. This class implements the Collection interface as well as the AbstractCollection class. It is a part of the Java Collection Framework.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.util.concurrent.*;
public class Demo {
   public static void main(String[] args) {
      ConcurrentLinkedQueue<String> clQueue = new ConcurrentLinkedQueue<String>();
      clQueue.add("Amy");
      clQueue.add("John");
      clQueue.add("May");
      clQueue.add("Harry");
      clQueue.add("Anne");
      System.out.println("The elements in ConcurrentLinkedQueue are: " + clQueue);
   }
}

The output of the above program is as follows −

Output

The elements in ConcurrentLinkedQueue are: [Amy, John, May, Harry, Anne]

Now let us understand the above program.

The ConcurrentLinkedQueue is created and then elements are added to it. Finally, it is displayed. A code snippet that demonstrates this is given as follows −

ConcurrentLinkedQueue<String> clQueue = new ConcurrentLinkedQueue<String>();
clQueue.add("Amy");
clQueue.add("John");
clQueue.add("May");
clQueue.add("Harry");
clQueue.add("Anne");
System.out.println("The elements in ConcurrentLinkedQueue are: " + clQueue);
raja
Updated on 30-Jul-2019 22:30:25

Advertisements