ConcurrentLinkedQueue in Java



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);
Updated on: 2019-07-30T22:30:25+05:30

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements