Java: ArrayBlockingQueue vs. 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();
}
ArrayBlockingQueue
和 LinkedBlockingQueue
的最大区别从构造函数中可以清楚地看出,一个具有 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
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01