by BehindJava

Which concurrent Queue implementation, should I use it in Java

Home » java » Which concurrent Queue implementation, should I use it in Java

In this tutorial we are going to learn about using concurrent Queue implementation should I use in Java.

From the JavaDocs:

A ConcurrentLinkedQueue is an appropriate choice when many threads will share access to a common collection and no null elements are allowed.

ArrayBlockingQueue is fixed-sized array holds elements that are inserted by producers and extracted by consumers and ArrayBlockingQueue is a classic example of Bounded Buffer. This class supports an optional fairness policy for ordering waiting producer and consumer threads.

public ArrayBlockingQueue(int capacity, boolean fair) 
            if (capacity < = 0)
                throw new IllegalArgumentException();
            this.items = new Object[capacity]; // Maintains a underlying array
            lock = new ReentrantLock(fair);
            notEmpty = lock.newCondition();
            notFull =  lock.newCondition();

LinkedBlockingQueue typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.

public LinkedBlockingQueue(int capacity) 
        if (capacity < = 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        last = head = new Node< E >(null);   // Maintains a underlying linkedlist. ( Use when size is not known )

Basically the difference between them are performance characteristics and blocking behavior.

Taking the easiest first, ArrayBlockingQueue is a queue of a fixed size. When you set the size as 10, and attempt to insert an 11th element, the insert statement will block until another thread removes an element. The fairness issue is what happens if multiple threads try to insert and remove at the same time (in other words during the period when the Queue was blocked).

A fairness algorithm ensures that the first thread that asks is the first thread that gets. Otherwise, a given thread may wait longer than other threads, causing unpredictable behavior (sometimes one thread will just take several seconds because other threads that started later got processed first). The trade-off is that it takes overhead to manage the fairness, slowing down the throughput.

The most important difference between LinkedBlockingQueue and ConcurrentLinkedQueue is that if you request an element from a LinkedBlockingQueue and the queue is empty, your thread will wait until there is something there. A ConcurrentLinkedQueue will return right away with the behavior of an empty queue.

Which one depends on if you need the blocking. Where you have many producers and one consumer, it sounds like it. On the other hand, where you have many consumers and only one producer, you may not need the blocking behavior, and may be happy to just have the consumers check if the queue is empty and move on if it is.