Java ArrayBlockingQueue 和 LinkedBlockingQueue 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18375334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:59:53  来源:igfitidea点击:

What is the Difference between ArrayBlockingQueue and LinkedBlockingQueue

javablockingqueueblockingcollection

提问by Java_Hyman

  1. What scenarios is it better to use an ArrayBlockingQueue and when is it better to use a LinkedBlockingQueue?
  2. If LinkedBlockingQueue default capacity is equal to MAX Integer, is it really helpful to use it as BlockingQueue with default capacity?
  1. 什么情况下使用 ArrayBlockingQueue 更好,什么时候使用 LinkedBlockingQueue 更好?
  2. 如果 LinkedBlockingQueue 默认容量等于 MAX Integer,将其用作具有默认容量的 BlockingQueue 真的有用吗?

回答by Fabian Barney

ArrayBlockingQueueis backed by an array that size will never change after creation. Setting the capacity to Integer.MAX_VALUEwould create a big array with high costs in space. ArrayBlockingQueueis always bounded.

ArrayBlockingQueue由一个数组支持,该数组在创建后永远不会改变。将容量设置为Integer.MAX_VALUE将创建一个空间成本高的大阵列。 ArrayBlockingQueue总是有界的。

LinkedBlockingQueuecreates nodes dynamically until the capacityis reached. This is by default Integer.MAX_VALUE. Using such a big capacity has no extra costs in space. LinkedBlockingQueueis optionally bounded.

LinkedBlockingQueue动态创建节点,直到capacity达到。这是默认的Integer.MAX_VALUE。使用如此大的容量并没有额外的空间成本。 LinkedBlockingQueue是可选有界的。

回答by Ankur Lathi

ArrayBlockingQueue<E>and LinkedBlockingQueue<E>are common implementations of the BlockingQueue<E>interface.

ArrayBlockingQueue<E>LinkedBlockingQueue<E>BlockingQueue<E>接口的常见实现 。

ArrayBlockingQueueis backed by arrayand Queueimpose orders as FIFO. head of the queue is the oldest element in terms of time and tail of the queue is youngest element. ArrayBlockingQueueis also fixed size bounded buffer on the other hand LinkedBlockingQueueis an optionally bounded queue built on top of Linked nodes.

ArrayBlockingQueue由 支持arrayQueue强制执行命令FIFO。就时间而言,队列的头部是最老的元素,队列的尾部是最年轻的元素。ArrayBlockingQueue另一方面,也是固定大小的有界缓冲区,它LinkedBlockingQueue是一个建立在链接节点之上的可选有界队列。

The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion because if capacity is unspecified, than it is equal to Integer.MAX_VALUE.

可选的容量绑定构造函数参数可作为一种防止队列过度扩展的方法,因为如果容量未指定,则它等于Integer.MAX_VALUE

Read more From here.

这里阅读更多内容。

Benchmark: http://www.javacodegeeks.com/2010/09/java-best-practices-queue-battle-and.html

基准:http: //www.javacodegeeks.com/2010/09/java-best-practices-queue-battle-and.html

回答by Evgeniy Dorofeev

Adding an element to ArrayBlockingQueue is supposed to be faster since it means only setting a reference to an element of the backing Object array, while adding an element to LinkedBlockingQueue means creating a Node and setting its item, prev and next fields. Besides, when we remove an element from LinkedBlockingQueue the removed Node becomes garbage which may influence app's performance.

向 ArrayBlockingQueue 添加元素应该更快,因为这意味着只设置对支持 Object 数组元素的引用,而向 LinkedBlockingQueue 添加元素意味着创建一个节点并设置其 item、prev 和 next 字段。此外,当我们从 LinkedBlockingQueue 中移除一个元素时,移除的 Node 会变成垃圾,这可能会影响应用程序的性能。

As for memory consumption ArrayBlockingQueue always holds an Object array with full capacity even when empty. On the other hand one element in LinkedBlockingQueue is a Node with an Object with 3 Object fields.

至于内存消耗,ArrayBlockingQueue 始终持有一个满容量的 Object 数组,即使是空的。另一方面,LinkedBlockingQueue 中的一个元素是一个具有 3 个对象字段的对象的节点。

回答by Ramesh Papaganti

ArrayBlockingQueue:

ArrayBlockingQueue

ArrayBlockingQueue is a bounded, blocking queue that stores the elements internally in an array. That it is bounded means that it cannot store unlimited amounts of elements. There is an upper bound on the number of elements it can store at the same time. You set the upper bound at instantiation time, and after that it cannot be changed.

ArrayBlockingQueue 是一个有界的阻塞队列,它将元素内部存储在一个数组中。它是有界的意味着它不能存储无限数量的元素。它可以同时存储的元素数量有一个上限。您在实例化时设置上限,此后无法更改。

LinkedBlockingQueue

链接阻塞队列

The LinkedBlockingQueue keeps the elements internally in a linked structure (linked nodes). This linked structure can optionally have an upper bound if desired. If no upper bound is specified, Integer.MAX_VALUE is used as the upper bound.

LinkedBlockingQueue 将元素内部保存在链接结构(链接节点)中。如果需要,该链接结构可以可选地具有上限。如果未指定上限,则使用 Integer.MAX_VALUE 作为上限。

Similarity

相似

The ArrayBlockingQueue/LinkedBlockingQueue stores the elements internally in FIFO (First In, First Out) order. The head of the queue is the element which has been in queue the longest time, and the tail of the queue is the element which has been in the queue the shortest time.

ArrayBlockingQueue/LinkedBlockingQueue 以 FIFO(先进先出)顺序在内部存储元素。队头是排队时间最长的元素,队尾是排队时间最短的元素。

Differences

差异

  • LinkedBlockingQueue has a putLock and a takeLock for insertion and removal respectively but ArrayBlockingQueue uses only 1 lock.
  • 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).
  • LinkedBlockingQueue 有一个 putLock 和一个 takeLock 分别用于插入和删除,但 ArrayBlockingQueue 只使用 1 个锁。
  • ArrayBlockingQueue 使用单锁双条件算法,LinkedBlockingQueue 是“双锁队列”算法的变体,它有 2 个锁 2 个条件( takeLock , putLock )。

Two Lock Queue algorithm is being used by LinkedBlockingQueue Implementation.Thus LinkedBlockingQueue's take and put can work concurrently, but this is not the case with ArrayBlockingQueue. The reason for using a single lock in ArrayBlockingQueue is ,ArrayBlockingQueue has to avoid overwriting entries so that it needs to know where the start and the end is. A LinkedBlockQueue doesn't need to know this as it lets the GC worry about cleaning up Nodes in the queue.

LinkedBlockingQueue 实现使用了两个锁队列算法。因此 LinkedBlockingQueue 的 take 和 put 可以并发工作,但 ArrayBlockingQueue 不是这种情况。在 ArrayBlockingQueue 中使用单锁的原因是,ArrayBlockingQueue 必须避免覆盖条目,因此它需要知道开始和结束的位置。LinkedBlockQueue 不需要知道这一点,因为它让 GC 担心清理队列中的节点。