Java:ArrayBlockingQueue 与 LinkedBlockingQueue

Java: ArrayBlockingQueue vs. LinkedBlockingQueue(Java:ArrayBlockingQueue 与 LinkedBlockingQueue)

本文介绍了Java:ArrayBlockingQueue 与 LinkedBlockingQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为,在大多数情况下,ArrayBlockingQueue 会比 LinkedBlockingQueue 执行得更好.但是,当数组中总是有足够的空间时就是这种情况......如果它已满,那么它是否会表现得这么好就不是很可预测了,因为它会阻塞试图将数据推送到队列中的线程.......

I think that, in most cases, the ArrayBlockingQueue will perform better than the LinkedBlockingQueue. However, that is the case when there is always enough room in the array... If it gets full, it's not very predictable whether it will perform so well, since it will block the thread that's trying to push data into the queue...

所以,我的问题是:BlockingQueue 是否有任何中间实现?比如说,一个 ArrayListBlockingQueue 还是一个 BucketListBlockingQueue?类似于数组列表的东西,这样队列可以动态增加容量,同时仍然可以从使用数组最终存储数据中获得合理的好处?

So, my question is: Is there any middle-ground implementation of BlockingQueue? Say, an ArrayListBlockingQueue or a BucketListBlockingQueue? Something like a list of arrays, so that the queue can increase in capacity dynamically, while still having a reasonable benefit from using array to ultimately store data?

推荐答案

1 .LinkedBlockingQueue(LinkedList实现,但不完全是LinkedList的JDK实现.它使用静态内部类节点来维护之间的链接元素)

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类用于维护Links

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

2 .ArrayBlockingQueue(数组实现)

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

ArrayBlockingQueueLinkedBlockingQueue 的最大区别从构造函数中可以清楚地看出,一个具有 Array 的底层数据结构,另一个具有 的底层数据结构链表.

Biggest difference between ArrayBlockingQueue and LinkedBlockingQueue is clear from constructor, one has an underlying data structure of Array and the other of LinkedList.

ArrayBlockingQueue 使用 单锁双条件算法和LinkedBlockingQueue是双锁队列"的变体;算法,它有 2 个锁 2 个条件(takeLock,putLock)

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

到目前为止,我对这两种实现进行了比较回到原来的问题,类似的问题在 并发邮件列表 在这个 doug Lea 谈到了 DynamicArrayBlockingQueue 这是 由 Dawid Kurzyniec 提供的实现.

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.

这篇关于Java:ArrayBlockingQueue 与 LinkedBlockingQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java:ArrayBlockingQueue 与 LinkedBlockingQueue

基础教程推荐