gpt4 book ai didi

java - 为什么 ArrayBlockingQueue 称为有界队列,而 LinkedBlockingQueue 称为无界阻塞队列?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:30:33 25 4
gpt4 key购买 nike

据我所知,链表和数组都可以无限增长,还是我错了?但是当我经历了 documentation in the Executor Service我看到这个:

Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to wait in the queue when all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created. (And the value of the maximumPoolSize therefore doesn't have any effect.)

LinkedBlockingQueue 具有定义的容量时,Unbounded Queue 属性会发生变化吗?

这是为 ArrayBlockingQueue 编写的:

Bounded queues. A bounded queue (for example, an ArrayBlockingQueue) helps prevent resource exhaustion when used with finite maximumPoolSizes, but can be more difficult to tune and control. Queue sizes and maximum pool sizes may be traded off for each other: Using large queues and small pools minimizes CPU usage, OS resources, and context-switching overhead, but can lead to artificially low throughput. If tasks frequently block (for example if they are I/O bound), a system may be able to schedule time for more threads than you otherwise allow. Use of small queues generally requires larger pool sizes, which keeps CPUs busier but may encounter unacceptable scheduling overhead, which also decreases throughput.

最佳答案

为什么您认为 ArrayBlockingQueue 可以无限增长?来自其 own documentation :

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 increased. 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.

换句话说,一旦它变满了,它就变满了——它不会增长。

您是否对 ArrayList 感到困惑 - 它也由数组支持,但根据需要扩展它?

So does the Unbounded Queue property changes when the LinkedBlockingQueue has a defined capacity ?

是的,因此它在 Javadocs 中被描述为“可选边界” .此外,文档指出(强调我的):

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.

关于java - 为什么 ArrayBlockingQueue 称为有界队列,而 LinkedBlockingQueue 称为无界阻塞队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11830664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com