ConcurrentLinkedQueue in Java with Examples

The ConcurrentLinkedQueue class in Java is a part of the Java Collection Framework. It belongs to java.util.concurrent package. It was introduced in JDK 1.5. It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts elements at the tail of the Queue in a FIFO(first-in-first-out) fashion. It can be used when an unbounded Queue is shared among many threads. This class does not permit null elements. Iterators are weakly consistent. This class and its iterator implement all of the optional methods of the Queue and Iterator interfaces.
Class Hierarchy:

java.lang.Object ↳ java.util.AbstractCollection ↳ java.util.AbstractQueue ↳ Class ConcurrentLinkedQueue

ConcurrentLinkedQueue in Java

Declaration:

public class ConcurrentLinkedQueue extends AbstractCollection implements Queue, Serializable

Here, E is the type of elements maintained by this collection.

Constructors of ConcurrentLinkedQueue

To construct a ConcurrentLinkedQueue, we need to import it from java.util.ConcurrentLinkedQueue.

1. ConcurrentLinkedQueue(): This constructor is used to construct an empty queue.

ConcurrentLinkedQueue clq = new ConcurrentLinkedQueue();

2. ConcurrentLinkedQueue(Collection c): This constructor is used to construct a queue with the elements of the Collection passed as the parameter.

ConcurrentLinkedQueue clq = new ConcurrentLinkedQueue(Collection c);

Below is a sample program to illustrate ConcurrentLinkedQueue in Java:

Example 1: