Java: ArrayBlockingQueue vs. LinkedBlockingQueue

为君一笑 提交于 2019-11-30 01:12:59
Vipin

1 . LinkedBlockingQueue ( LinkedList Implementation but Not Exactly JDK Implementation of LinkedList It uses static inner class Node to maintain Links between elements )

Constructor for LinkedBlockingQueue
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 )
}

Node class Used to Maintain Links

static class Node<E> {
    E item;
    Node<E> next;
    Node(E x) { item = x; }
}

2 . ArrayBlockingQueue ( Array Implementation )

Constructor for ArrayBlockingQueue

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();
}

IMHO Biggest Difference between ArrayBlockingQueue and LinkedBlockingQueue is clear from constructor one has underlying data structure Array and other linkedList.

ArrayBlockingQueue uses single-lock double condition algorithm and LinkedBlockingQueue is variant of the "two lock queue" algorithm and it has 2 locks 2 conditions ( takeLock , putLock)

Till now I gave comparison between these 2 implementations Coming back to original question , Similar question was asked in concurrency mailing list in this doug Lea talks about DynamicArrayBlockingQueue which is implementation provided by Dawid Kurzyniec.

My 2 cents:

To start with, the bottom line here is you don't really care about the difference here because even when you are using a plain LinkedBlockingQueue, the performance is good enough when you are delivering some micro-second level systems. So the performance difference here isn't really that great.

If you are writing a mission-critical high performance system and you are using queues to pass messages between threads, you can always estimate the queue size needed by [Queue Size] = [Max acceptable delay] * [Max message rate]. Anything which can grow beyond such capacity means you suffer from a slow consumer problem. In a mission critical application, such delay means your system is malfunctioning. Some manual process might be needed to make sure the system is running properly.

In case your system isn't mission critical, you can simply pause (block) the publisher until some consumers are available.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!