Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Using Java ConcurrentLinkedQueue for Lock-Free Thread-Safe Queues

Learn how to use Java's ConcurrentLinkedQueue for lock-free, thread-safe queue operations, including iteration behavior, performance tradeoffs, and common pitfalls.

~5 min readJava
#ConcurrentLinkedQueue#Thread Safety#Lock-Free#Java Concurrency
Illustration of a lock-free queue with multiple threads accessing nodes concurrently, representing Java's ConcurrentLinkedQueue.

ConcurrentLinkedQueue is Java's lock-free, unbounded, thread-safe queue. It implements the Queue interface using a linked-node structure, allowing multiple threads to add and remove elements without blocking each other. Operations do not wait for space or data; they either complete immediately or retry internally with compare-and-set (CAS) loops. This article explains how ConcurrentLinkedQueue works, how to use it, and the tradeoffs to consider.

Understanding the Lock-Free Thread-Safety Model

ConcurrentLinkedQueue achieves thread safety without explicit locks. Internally, it uses atomic operations on node references to update the head and tail pointers. The algorithm is based on Michael and Scott's lock-free queue design, which lets multiple threads enqueue and dequeue concurrently without contending for a single lock.

Because no thread waits on a lock, there is no lock-based deadlock or priority inversion. However, operations do not support waiting for elements to become available. If a consumer tries to poll an empty queue, it immediately returns null. This is a fundamental difference from blocking queues like LinkedBlockingQueue, where take() waits until an element appears.

Basic Usage: Adding and Removing Elements

Creating a ConcurrentLinkedQueue is straightforward. The class has two constructors: one creates an empty queue, and one accepts a Collection to initialize the queue with existing elements.

java
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>(); // Add elements queue.offer("first"); queue.add("second"); // add() is equivalent to offer() for this queue // Remove and return the head String head = queue.poll(); // returns "first" // Peek without removing String peeked = queue.peek(); // returns "second"

The offer and add methods are functionally identical for this class; both insert an element at the tail. The poll method removes and returns the head, or null if the queue is empty. peek returns the head without removing it, also returning null for an empty queue.

Unlike LinkedList, ConcurrentLinkedQueue does not allow null elements. Attempting to add null throws a NullPointerException. This is a deliberate design choice to avoid ambiguity in methods like poll and peek, where null indicates an empty queue.

Iteration and Weakly Consistent Iterators

The iterators returned by ConcurrentLinkedQueue are weakly consistent. This means they do not throw ConcurrentModificationException if the queue is modified during iteration, and they may or may not reflect elements added or removed after the iterator was created. The iterator is guaranteed to traverse elements that were present at the moment the iterator was constructed, but it may also see elements added later, depending on timing.

java
ConcurrentLinkedQueue<Integer> numbers = new ConcurrentLinkedQueue<>(); numbers.add(1); numbers.add(2); Iterator<Integer> it = numbers.iterator(); numbers.add(3); // modification after iterator creation while (it.hasNext()) { System.out.println(it.next()); // may print 1, 2, and possibly 3 }

This behavior is useful for lock-free traversal in concurrent environments, but it does not provide a point-in-time snapshot. If you need an exact snapshot, you would need external synchronization while copying or a data structure designed for snapshot semantics.

Comparing ConcurrentLinkedQueue with Other Queue Implementations

Choosing the right queue depends on your concurrency requirements and whether blocking is acceptable. The table below highlights key differences.

ImplementationBlocking BehaviorThread SafetyCapacityUse Case
ConcurrentLinkedQueueNon-blockingLock-freeUnboundedHigh-throughput, non-blocking producer-consumer
LinkedBlockingQueueBlocking on take/putLock-basedOptional boundedProducer-consumer with backpressure
ArrayBlockingQueueBlocking on take/putLock-basedBoundedBounded buffer with fixed capacity
ConcurrentLinkedDequeNon-blockingLock-freeUnboundedDouble-ended operations

ConcurrentLinkedQueue is ideal when you need maximum throughput and can tolerate consumers that must poll for new elements rather than waiting. If you need a blocking take() that waits for data, use LinkedBlockingQueue or ArrayBlockingQueue. If you need a bounded queue to prevent memory exhaustion, ArrayBlockingQueue is a better fit.

Performance Characteristics and Memory Behavior

Because ConcurrentLinkedQueue uses CAS operations, it generally performs well under high contention, but it is not always the fastest choice for low-contention scenarios. The lock-free algorithm avoids the overhead of acquiring and releasing locks, but each operation involves multiple atomic reads and writes. For very small queues or low concurrency, a simpler synchronized structure may be more convenient, though it will not offer the same scalability.

One important performance caveat is the size() method. Unlike LinkedList, ConcurrentLinkedQueue does not maintain a size counter because doing so would require atomic updates on every modification, which is expensive. Instead, size() traverses the entire queue to count elements, making it an O(n) operation. Calling size() frequently in a loop can degrade performance significantly. If you need an approximate count, consider maintaining your own AtomicInteger and updating it alongside queue operations, but be aware that it may not be perfectly synchronized.

Memory usage is another consideration. Each element is wrapped in a node object with two references (next and item), adding overhead compared to an array-backed queue. For queues holding many small objects, this overhead can be substantial. However, the linked structure allows constant-time insertion and removal at both ends without shifting elements.

Common Pitfalls and Usage Considerations

A frequent mistake is expecting ConcurrentLinkedQueue to block when the queue is empty. Since it is non-blocking, poll() returns null immediately. If your consumer needs to wait for elements, you must implement waiting with another coordination mechanism, such as a Semaphore, or switch to a blocking queue.

Another pitfall is relying on size() for control flow. Because size() is O(n), using it to decide whether to poll can cause race conditions. For example, checking if (!queue.isEmpty()) { queue.poll(); } is not atomic; another thread could remove the element between the check and the poll. Instead, simply call poll() and handle the null result.

Also note that ConcurrentLinkedQueue is not a Deque; it only supports FIFO ordering. If you need LIFO or double-ended access, look at ConcurrentLinkedDeque or other concurrent deque implementations.

When to Choose ConcurrentLinkedQueue

Select ConcurrentLinkedQueue when your application requires a thread-safe, unbounded queue with non-blocking semantics. Typical use cases include:

  • Work queues where producers add tasks and consumers poll for work without waiting.
  • Event delivery systems where multiple threads publish events and one or more threads process them.
  • Situations where blocking could cause deadlock or where you need to avoid thread suspension.

Avoid it when you need bounded capacity, blocking operations, or when you need a consistent snapshot for iteration. In those cases, a blocking queue or a synchronized collection with explicit locking is more appropriate.

For most high-throughput, non-blocking producer-consumer scenarios, ConcurrentLinkedQueue provides a solid balance of scalability and simplicity. Understanding its lock-free nature and its tradeoffs will help you use it effectively without falling into common concurrency traps.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.