Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why all thread pool blocking queues use LinkedBlockingQueue

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about why thread pool blocking queues use LinkedBlockingQueue. Many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something from this article.

Whether it is several thread pools provided by Executors or thread pools provided by Spring, you will find that blocking queues use LinkedBlockingQueue instead of ArrayBlockingQueue

LinkedBlockingQueue

Implemented using a single linked list, providing three constructors

LinkedBlockingQueue () no-parameter constructor, linked list length is Integer.MAX_VALUE

LinkedBlockingQueue (int capacity) specifies the capacity length

LinkedBlockingQueue (Collection c) does not specify a length, that is, the default length is Integer.MAX_VALUE, which provides initialization elements

The linked list node consists of Node objects, each Node has an item variable to store the element, and the next variable points to the next node

When performing put, put the element at the end of the linked list; when take, fetch the element from the header

The two operations have a lock putLock and takeLock, which do not affect each other and can be performed at the same time.

/ * * Lock held by take, poll, etc * /

Private final ReentrantLock takeLock = new ReentrantLock ()

/ * * Lock held by take, poll, etc * /

Private final ReentrantLock takeLock = new ReentrantLock ()

ArrayBlockingQueue

Using array implementation, three constructors

ArrayBlockingQueue (int capacity) specifies the length

ArrayBlockingQueue (int capacity, boolean fair) specifies the length and whether to use the FIFO sequence to enter and leave the queue

ArrayBlockingQueue (int capacity, boolean fair, Collection c) specifies the length, queue order, initial elements

As you can see from the constructor, ArrayBlockingQueue must specify the initialization length. If the thread pool uses the queue, memory is wasted if the specified length is large, and the concurrency of the queue with small length is not high. When the array is full, the put operation can only block waiting or return false.

ArrayBlockingQueue defines only one Lock,put and take using the same lock, not at the same time.

/ * * Main lock guarding all access * /

Final ReentrantLock lock

LinkedBlockingQueue does not need to specify length, and different locks are used to put in and take out elements, which do not affect each other, high efficiency and strong versatility.

ArrayBlockingQueue must specify length, large waste of memory, low performance, and low efficiency when using the same lock.

After reading the above, do you have any further understanding of why thread pool blocking queues use LinkedBlockingQueue? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report