Why is ArrayBlockingQueue called a restricted queue and LinkedBlockingQueue called an unlimited blocking queue?

As far as I know, both the linked list and the array can grow without restrictions or am I mistaken? But when I looked at the documentation in the Executor service , I see the following:

Unlimited Queues. Using an unlimited queue (for example, LinkedBlockingQueue without a predefined capacity) will cause new queue waiting tasks when all corePoolSize threads are busy. This way, no more than corePoolSize threads will be created. (And the value of maximumPoolSize therefore has no effect.)

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

And this is written for ArrayBlockingQueue :

Limited queues. A limited queue (such as ArrayBlockingQueue) helps prevent resource depletion when used with finite maximumPoolSizes, but it can be harder to configure and control. Queue sizes and maximum pool sizes can be sold for each other: Using large queues and small pools minimizes CPU usage, OS resources and context switching, but can lead to artificially low bandwidth. If tasks are often blocked (for example, if they are I / O), the system can schedule time for more threads than you otherwise allow. Using small queues usually requires large pool sizes, which increases processor performance, but scheduling overheads may be unacceptable, which also reduces throughput.

+8
source share
5 answers

Why do you think ArrayBlockingQueue can grow without limit? From own documentation :

This is the classic "limited buffer" in which a fixed-size array contains elements inserted by manufacturers and retrieved by consumers. Once created, capacity cannot be increased. Attempts to place an element in full queue will block the operation; Attempts to capture an item from an empty queue will likewise be blocked.

In other words, as soon as it is filled, it is filled - it does not grow.

Do you happen to get confused with an ArrayList , which is also supported by an array, but which extends it as needed?

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

Yes, therefore why it is described as "not necessarily limited" in Javadocs . In addition, the documents indicate that (my attention):

An additional constructor argument related to bandwidth is used to prevent excessive queue expansion. Capacity, if not specified, is Integer.MAX_VALUE. Linked nodes are dynamically created each time you insert , if this does not result in queues being exceeded .

+11
source

Javadoc for LinkedBlockingQueue states:

Optional limited blocking queue based on linked nodes. [...]

The optional constructor argument related to capacity serves as a way to prevent the queue from expanding too much. Capacity, if not specified, is Integer.MAX_VALUE.

Javadoc ArrayBlockingQueue states:

The limited locking queue supported by the array. [...]

This is the classic "limited buffer" that contains an array of fixed-sized items inserted by manufacturers and retrieved by consumers. once created, capacity cannot be increased

Thus, LinkedBlockingQueue can be limited or unlimited, while ArrayBlockingQueue is always limited.

+4
source

As far as I know, both the linked list and the array can grow without restrictions or am I mistaken

Linked list as unlimited size. The array has a fixed size. ArrayList wraps an array and replaces it when it needs more.

So the Unbounded Queue property changes when LinkedBlockingQueue has a certain capacity

When LinkedBlockingQueue has maximum capacity, it is limited, but it is not used by default.

+3
source

From the documentation for ArrayBlockingQueue

The limited blocking queue supported by the array. This queue orders FIFO elements (first-in-first-out). The head of the queue is the element that was the longest in the queue. The tail of the queue is the element that was in the queue in the shortest time. New items are inserted at the tail of the queue, and queue lookup operations get items at the beginning of the queue.

If you notice that all ArrayBlockingQueue constructors have bandwidth because this class was intended to be limiting. This choice was made because if you want a parallel queue, you probably don't want the overhead associated with resizing an ArrayList. Therefore, if you want an unlimited LinkedBlockingQueue queue to be the best option, since it is not associated with this overhead.

+3
source

another answer is very right! I provide another way to explain. well, I also confuse the term "unrelated and related." you can look at the source code.

  /** The queued items */ final Object[] items; /** items index for next take, poll, peek or remove */ int takeIndex; /** items index for next put, offer, or add */ int putIndex; /** Number of elements in the queue */ int count; 

from the source code, we can see that the array is final, so we can not change the size of the array. if we use LinkedBlockingQueue, we can always add more elements ... and in the source code the following link is not final. NOTE. Theoretically, LinkedBlockingQueue is not unlimited. because it can only store MAX_INTEGER minus 8 elements. from javadoc unlimited queue - PriorityBlockingQueue. but PriorityBlockingQueue can also only store MAX_INTEGER -8 elements. so I think there is no perfect unlimited lineup ...

0
source

Source: https://habr.com/ru/post/922223/


All Articles