What is better LinkedBlockingQueue without limits or LinkedBlockingQueue with capacity

I am using LinkedBlockingQueue as a workqueue in ThreadPoolExecutor. The problem is that I have to use a limited LinkedBlockingQueue or an unlimited LinkedBlockingQueue. I overridden the execute ThreadPoolExecutor method and no longer ran into the problem of creating threads after the size of the main pool.

So please tell me that it is better to use LinkedBlockingQueue limited or unlimited.

Thank you Tushar

+6
source share
3 answers

An unlimited queue is a safe way to ensure that no task is rejected, or use a limited queue with a capacity that is so large that it can hold the maximum number of tasks that can appear in your application. It depends on the design of your application. I think that if you understand (discuss with the architect) the design of the application, you can determine the size of the queue. As for the memory and the processor, if you do not add tasks to the queue, they will not increase and will be the same for both - unlimited or limited. (Tested in demo application)

public static void main(String[] args) { LinkedBlockingQueue<Runnable> r = new LinkedBlockingQueue<Runnable>(11); while(true) { // r.offer(new Task(1)); } } 

just play with the size to check.

+4
source

Unlimited LinkedBlockingQueue is basically a limited queue with java.lang.Integer.MAX_VALUE capacity. So yes, as mentioned in the comments, use a limited or unlimited queue based on your needs, and not for performance, as size checks occur regardless of whether you specify a limit or not.

As always, if you know the bandwidth in advance, I would recommend that you profile unlimited use of a queue with a limited bandwidth with a given bandwidth, although I would not recommend going along this route if you have no evidence that the queue is the one that causes the problem in your application.

+3
source

If you can estimate how many maximum number of pending items can be in the queue, it is better to use a limited queue. Themes into which items are inserted in the queue can know if the queue is full after the estimated queue size.

It all depends on the task you want to complete. If you want to create threads that insert items in the queue to wait after the maximum number of pending items in the queue, you should consider a limited queue.

Limited queues will be better in terms of memory and processor, since only a limited number of elements can be in the queue (memory advantage) and will create threads that insert elements into the queue to wait if the queue is full (CPU advantage). Overall performance will be improved.

This will be of great advantage if the speed of the queue in the queue is not equal to the speed of decoupling.

0
source

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


All Articles