ConcurrentLinkedQueue in Java

ConcurrentLinkedQueue in Java

The ConcurrentLinkedQueue is part of the java.util.concurrent package in Java. It is a thread-safe, non-blocking, and FIFO (First-In-First-Out) queue based on linked nodes. Due to its non-blocking algorithm, it allows concurrent and scalable operations.

1. Key Features of ConcurrentLinkedQueue

  • Non-blocking: Uses CAS (Compare-And-Swap) operations, enabling high concurrency without blocking.
  • Thread-safe: It is inherently thread-safe and can be safely accessed and modified by multiple threads.
  • Null Restriction: Unlike some other collections, ConcurrentLinkedQueue does not allow null elements.

2. Basic Operations

Creating a ConcurrentLinkedQueue:

ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>(); 

Adding Elements:

queue.offer("element1"); queue.offer("element2"); 

or

queue.add("element"); 

Fetching and Removing Elements:

String element = queue.poll(); 

Peeking Elements (retrieve but not remove):

String peekedElement = queue.peek(); 

3. Iterators

Iterators provided by ConcurrentLinkedQueue are weakly consistent. This means they won't throw a ConcurrentModificationException if the queue is modified while being iterated, but they might not reflect all changes made to the queue since they were created.

for (String item : queue) { System.out.println(item); } 

4. Size and Checking for Elements

To check if the queue is empty:

boolean isEmpty = queue.isEmpty(); 

To get the number of elements:

int size = queue.size(); 

However, remember that size() might be an expensive operation in a highly concurrent environment since it has to traverse the elements to count them.

To check if the queue contains an element:

boolean contains = queue.contains("element1"); 

5. Concurrent Modifications

Being thread-safe, multiple threads can concurrently modify the ConcurrentLinkedQueue:

Thread producer = new Thread(() -> { for (int i = 0; i < 5; i++) { queue.offer("Produced " + i); } }); Thread consumer = new Thread(() -> { while (queue.peek() != null) { System.out.println("Consumed: " + queue.poll()); } }); producer.start(); consumer.start(); producer.join(); consumer.join(); 

6. Performance Considerations

  • ConcurrentLinkedQueue offers better performance under contention compared to a synchronized version of queues like LinkedList.
  • The non-blocking nature ensures threads are not kept in a waiting state, leading to better throughput in concurrent scenarios.

7. When to use ConcurrentLinkedQueue

Use ConcurrentLinkedQueue when:

  • You have a high-concurrency environment with multiple threads producing and consuming data.
  • You want a FIFO ordering.
  • You prefer non-blocking algorithms over locked or synchronized ones.

Conclusion

ConcurrentLinkedQueue provides a powerful, scalable, and thread-safe queue ideal for concurrent applications. Its non-blocking characteristics allow efficient operations even under high contention, making it an excellent choice for high-throughput, real-time applications requiring concurrent data structures.


More Tags

asp.net-3.5 pyqtgraph data-fitting using document linked-tables keras python jackson angular2-forms

More Programming Guides

Other Guides

More Programming Examples