为什么ArrayBlockingQueue必须是有界的,而LinkedBlockingQueue不需要呢?

huangapple go评论128阅读模式
英文:

Why ArrayBlockingQueue must have bound, while LinkedBlockingQueue not?

问题

当我实例化ArrayBlockingQueue时,必须在其构造函数中同时设置int capacity。但对于LinkedBlockingQueue则不适用相同的情况。我想知道这背后的原因是什么?

当然,我也可以在LinkedBlockingQueue中设置边界,但这是最优的。为什么在这种情况下它是最优的,而在ArrayBlockingQueue中却不是?难道ArrayBlockingQueue不能像ArrayList一样在默认情况下具有初始容量吗?

英文:

When I instantiate ArrayBlockingQueue, I must also put int capacity in its constructor. Same thing doesn't apply for LinkedBlockingQueue. I am interested in why is that?

Of course, I can also put bound in LinkedBlockingQueue, but it is optimal. Why is it optimal here, and not in ArrayBlockingQueue? Couldn't ArrayBlockingQueue have initial capacity on default like ArrayList has?

答案1

得分: 2

ArrayBlockingQueue的文档说:

这是一个经典的"有界缓冲区",其中一个固定大小的数组保存生产者插入的元素,并由消费者提取。 一旦创建,容量无法更改。 尝试将元素放入已满队列将导致操作阻塞;尝试从空队列中取出元素将同样阻塞。

由于容量是固定的且无法更改,决定队列何时开始阻塞是该类的用户的责任。

LinkedBlockingQueue的文档说:

基于链接节点的可选有界阻塞队列。该队列按照FIFO(先进先出)顺序排列元素。队列的头是在队列上停留时间最长的元素。队列的尾是在队列上停留时间最短的元素。新元素插入到队列的尾部,队列检索操作在队列的头部获取元素。与基于数组的队列相比,链式队列通常具有更高的吞吐量,但在大多数并发应用中性能较不可预测。

可选的容量限制构造函数参数用作防止队列过度扩展的方法。 如果未指定容量,则容量等于Integer.MAX_VALUE。除非这会使队列超过容量,否则在每次插入时动态创建链接节点。

在这种情况下,基于提供的容量进行阻塞。如果指定了容量,那么阻塞容量的执行方式与ArrayBlockingQueue相同。

英文:

The documentation for ArrayBlockingQueue says.

>This is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. Once created, the capacity cannot be changed. Attempts to put an element into a full queue will result in the operation blocking; attempts to take an element from an empty queue will similarly block.

Since the capacity is fixed and can't change, it is up to the user of the class to decide when the queue should start to block.

The documentation for LinkedBlockQueue says

> An optionally-bounded blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.

>The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion. The capacity, if unspecified, is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity.

In this case, the blocked based on a supplied capacity. If specified, that blocking capacity is enforced the same as the ArrayBlockingQueue

huangapple
  • 本文由 发表于 2020年8月26日 23:30:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63600889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定