Concurrent queue Queue usage scenario summary

Concurrent queue ConcurrentLinkedQueue, blocking queue AraayBlockingQueue, blocking queue LinkedBlockingQueue difference and usage scenario summary

 

The difference and connection between the three :  contact, all three are thread-safe. The difference is concurrency and blocking. The former is a concurrent queue, and because the cas algorithm is used, it can be processed with high concurrency; the latter two use a lock mechanism, so it is blocking. The point to note is that the former uses the cas algorithm, although it can be highly concurrent, but the characteristics of the cas cause the danger of the operation. You can check the cas algorithm for how dangerous it is (but some multi-consumption queues still use it, see the reasons below. description in the scene) 

 

The difference between the latter two : contact, the second and third are blocking queues, both use locks, and both have a blocking container Condition , which blocks the fetch operation when the capacity is empty and the write operation when the capacity is full. The difference is that the second is a whole lock, and the third is two locks. Therefore, the locking mechanism of the second and third is different. The throughput of the third is higher than that of the second , and the concurrency performance is also higher than that of the second .

The specific information of the latter two : LinkedBlockingQueue is an implementation of BlockingQueue that uses Link List . It uses two different locks for the head and tail (fetch and add operations), which improves throughput compared to ArrayBlockingQueue . It is also a blocking container, suitable for implementing the " consumer producer " pattern.

ArrayBlockingQueue is an array implementation of BlockingQueue . It uses a global lock to read and write operations to the queue in parallel, and uses two Conditions to block the fetch operation when the capacity is empty and the write operation when the capacity is full.

 Because LinkedBlockingQueue uses two independent locks to control data synchronization, two operations of access and access can be performed in parallel, thereby improving concurrency efficiency. However, ArrayBlockingQueue uses a lock, which causes two operations to compete for a lock, which makes the performance relatively low. LinkedBlockingQueue can not set the queue capacity, the default is Integer.MAX_VALUE. It is easy to cause memory overflow, and its value should generally be set.

Summary of usage scenarios:

The advantages of applying blocking queues: No additional synchronization is required when multiple threads operate a common queue. In addition, the queue will automatically balance the load, that is, the processing on the other side (both sides of production and consumption) will be blocked if it is processed quickly, thereby reducing the processing on both sides. The speed difference, the feature of automatic load balancing, makes it possible to use it for multi-producer queues, because if you generate too much (the queue is full), you will block and wait until the consumer consumes the queue and you can continue to produce. ConcurrentLinkedQueue  is an appropriate choice when many threads share access to a common  collection  .

LinkedBlockingQueue is mostly used for task queues (single-threaded release tasks, stop waiting for blocking when the task is full, and start the load release task when the task is completed and the consumption is less)

ConcurrentLinkedQueue   is mostly used for message queues (multiple threads send messages, just send them first, ignoring the concurrency- cas feature)

 

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

Single producer, single consumer  with  LinkedBlockingqueue  

Multiple producers, single consumer   with  LinkedBlockingqueue  

Single producer , multiple consumers   use  ConcurrentLinkedQueue

Multi-producer , multi-consumer   with  ConcurrentLinkedQueue

 

To summarize the above :

For example, in the message queue, many clients send messages, which are put into the queue according to the order sent by the client , and the ones sent first are put in first, and then because the queue is first-in-first-out, it comes out one by one, so it does not involve thread safety issues, so Use LinkedBlockingqueue   queue. For example, take the example of the message queue above. Since the queues come out one by one, when a message protocol body comes out, the thread pool allocates a thread to process the message body. This message body cannot be shared or shared for the thread pool. The problem is that multiple threads will not grab the same message body to execute, so there is no need to use a thread-safe queue structure; then if there is a situation, the queue is still coming out one by one, but the element that comes out is The thread pool is shared, that is, all threads need to use this element from the queue, that is, the situation where multiple consumers consume the same thing, so it is necessary to use a thread-safe queue, namely ConcurrentLinkedQueue .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325982391&siteId=291194637