CKTIS / Java
ConcurrentLinkedQueue — concurrent FIFO queue
Learn ConcurrentLinkedQueue and its add, isEmpty, and poll methods for insertion, empty checks, and safe retrieval.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
ConcurrentLinkedQueue<E> queue = new ConcurrentLinkedQueue<>();
queue.add(element); // boolean
queue.isEmpty(); // boolean
queue.poll(); // E or nullConcurrentLinkedQueue<E> is a thread-safe, unbounded FIFO queue using linked nodes and a non-blocking algorithm.
ConcurrentLinkedQueue.add(E) appends an element and returns true. Passing null throws NullPointerException. Unbounded capacity does not mean unlimited memory.
ConcurrentLinkedQueue.poll() removes and returns the head, or returns null when empty; it does not wait for an element.
ConcurrentLinkedQueue.isEmpty() checks emptiness but reserves nothing. An isEmpty() then poll() sequence is not atomic: another thread can empty the queue between calls. Retrieve with poll() and check its result.
Examples
import java.util.concurrent.ConcurrentLinkedQueue;
public class Main {
public static void main(String[] args) {
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
System.out.println(queue.isEmpty());
System.out.println(queue.add("A"));
queue.add("B");
String item;
while ((item = queue.poll()) != null) {
System.out.println(item);
}
System.out.println(queue.poll());
System.out.println(queue.isEmpty());
}
}Result
true true A B null true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.