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

How to use ConcurrentLinkedQueue and LinkedBlockingQueue

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about how to use ConcurrentLinkedQueue and LinkedBlockingQueue. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

In Java multithreaded applications, queue utilization is very high, and the preferred data structure of most production and consumption models is queue (first-in, first-out). Thread-safe Queue provided by Java can be divided into blocking queue and non-blocking queue, in which the typical example of blocking queue is BlockingQueue and the typical example of non-blocking queue is ConcurrentLinkedQueue. In practical application, we should choose blocking queue or non-blocking queue according to the actual needs.

Note: what is thread safety? First of all, we should be clear about this. Thread safety means that multiple threads access the same code without producing uncertain results.

The difference between parallelism and concurrency

1. Parallelism means that both perform the same thing at the same time, such as a race, where both people are constantly running forward.

2. Concurrency means that when resources are limited, the two take turns to use resources alternately. For example, a section of road (single-core CPU resources) can only pass one person at the same time. After A walks for a period, it will be handed over to BMIT B and continue to A for alternate use. The purpose is to improve efficiency.

LinkedBlockingQueue

Because the LinkedBlockingQueue implementation is thread-safe and implements features such as first-in, first-out, and so on, it is the first choice for producers and consumers. LinkedBlockingQueue can specify capacity or not. If not, the default maximum is Integer.MAX_VALUE, which mainly uses put and take methods. Put method will block until queue members are consumed when the queue is full, and take method will block until queue members are put in when the queue is empty.

ConcurrentLinkedQueue

ConcurrentLinkedQueue is a security implementation of Queue. Elements in queue are sorted according to FIFO principles. CAS operation is used to ensure the consistency of elements.

LinkedBlockingQueue is a thread-safe blocking queue, which implements the BlockingQueue interface, and the BlockingQueue interface inherits from the java.util.Queue interface, and adds take and put methods to this interface, which are the blocking versions of queue operations.

Running result:

Costtime 2360ms

After switching to while (queue.size () > 0)

Running result:

Cost time 46422ms

The result is so different, look at the ConcurrentLinkedQueue API original. Size () is to traverse the collection, no wonder it is so slow, so try to avoid using size and use isEmpty ().

To sum up, under the lack of performance testing, the programming requirements of the unit should be more strict, especially in the production environment.

Use the scene:

The benefits of blocking queues: multithreaded operations on common queues do not require additional synchronization, and the queues automatically balance the load, that is, where processing is fast (both production and consumption) will be blocked, thus reducing the processing speed gap between the two sides, automatic load balancing this feature makes it can be used in multi-producer queues, because you generate more (the queue is full), you have to block and wait You can not continue production until consumer consumption makes the queue dissatisfied. When many threads share access to a common collection, ConcurrentLinkedQueue is an appropriate choice.

LinkedBlockingQueue is mostly used for task queues (single thread publishes tasks, stops waiting for blocking when the task is full, and starts to load the publishing task when the task is completed and consumes less)

ConcurrentLinkedQueue is mostly used for message queues (multiple threads send messages first at random, regardless of the concurrent-cas feature)

Multiple producers are acceptable for LBQ performance, but multiple consumers can't. MainLoop needs a timeout mechanism, otherwise idling, cpu will soar. LBQ just provides the interface of timeout, which is more convenient to use. If CLQ, then I need to receive and process sleep.

After reading the above, do you have any further understanding of how to use ConcurrentLinkedQueue and 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