Comparison of several common concurrent queues in Java

Jiedao jdon 2021-05-04 16:50:50
comparison common concurrent queues java


In multithreaded applications , Queues need to handle multiple concurrent producers - Consumer solutions . The correct selection of concurrent queues is very important to achieve good performance in our algorithm . 

First , We'll see some important differences between blocking queues and non blocking queues . then , We'll look at some implementations and best practices .

 

BlockingQueue Provides a simple thread safety mechanism . In this queue , Threads need to wait for queue availability . Producers will wait for available capacity before adding elements , And consumers will wait until the queue is empty . In order to implement this blocking mechanism ,BlockingQueue The interface is in the normal Queue Two functions are provided on the basis of function :put and take. These functions are equivalent to the standard Queue Medium add and remove.

 

ArrayBlockingQueue

This queue uses arrays internally . therefore , It's a bounded queue , This means it has a fixed size . Suitable for producers / The consumer ratio is usually very low , We assign time-consuming tasks to multiple worker. Because this queue cannot grow indefinitely , So if there is a memory problem , You need to set the size limit as a security threshold .

ArrayBlockingQueue Yes put and take All operations use a lock . This ensures that the entry is not overridden , But it reduces performance .

 

LinkedBlockingQueue

LinkedBlockingQueue Use Linked list variant , Each queue item is a new node . Although this allows queues to be unrestricted in principle , But it still has Integer.MAX_VALUE The hard limit of . We can use constructors LinkedBlockingQueue(int capacity) Set queue size .

Queues use different locks put and take operation . So the two operations can be done in parallel and increase throughput .

because LinkedBlockingQueue It can be bounded or unbounded , Why should we use ArrayBlockingQueue? Every time an item is added or removed from the queue ,LinkedBlockingQueue Both need to assign and deallocate nodes . therefore , If the queue grows and shrinks rapidly , be   ArrayBlockingQueue Maybe a better choice .

It is said that LinkedBlockingQueue The performance of is unpredictable . let me put it another way , We always need to dissect our solutions to make sure we use the right data structures .

 

PriorityBlockingQueue

When we need to consume data in a specific order ,PriorityBlockingQueue Is our preferred solution . So ,PriorityBlockingQueue Using array based binary heaps .

Although a single locking mechanism is used internally , however take Operation can be done with put Simultaneous operation . This can be done with a simple spin lock .

A typical use case is to use tasks with different priorities . We don't want low priority tasks to replace high priority tasks .

 

DelayQueue

When users can only take When an expired data item , We use DelayQueue . Interestingly , It is used internally PriorityQueue To sort data items by their expiration time .

Because this is not a universal queue , So it can't cover ArrayBlockingQueue or LinkedBlockingQueue So many scenes . for example , We can use this queue to implement a simple event loop , Similar to in NodeJS Event loop found in . We put asynchronous tasks in queues , For subsequent processing when they are due .

 

LinkedTransferQueue

LinkedTransferQueue The introduction of a transfer  Method . Although other queues are usually blocked when producing or consuming data items , but LinkedTransferQueue  Allow producers to wait for data items to be consumed .

When we need to make sure that a particular item placed in the queue has been consumed by the consumer take When you take it away , have access to LinkedTransferQueue. Again , We can use this queue to implement a simple back pressure algorithm . actually , By stopping producers until they consume , Consumers can drive the resulting message flow .

 

SynchronousQueue

A normal queue usually contains many data items , but SynchronousQueue There is always only one project at most . let me put it another way , We need to SynchronousQueue As a simple way to exchange some data between two threads .

When we have two threads that need to access shared state , We usually associate them with CountDownLatch Or other synchronization mechanisms . By using SynchronousQueue, We can avoid this kind of manual synchronization of threads .

 

ConcurrentLinkedQueue

ConcurrentLinkedQueue It is the only non blocking queue in this paper , therefore , It provides a kind of “ No waiting ” Algorithm , among add and poll It's thread safe , And return immediately . This queue uses CAS(Compare-And-Swap) Instead of locks .

In the internal , It's based on Maged M. Michael and Michael L. Scott Of Simple , Fast and practical non blocking and blocking concurrent queue algorithm .

For the modern world where blocking data structures are often forbidden Reaction system , It's the ideal choice .

On the other hand , If our consumers end up in a cycle of waiting , We should choose blocking as a better choice .

版权声明
本文为[Jiedao jdon]所创,转载请带上原文链接,感谢
https://javamana.com/2021/05/20210504164657411p.html

  1. Realization of reactor Kafka through spring boot Webflux
  2. RPC框架设计----Socket与I/0模型
  3. Problems in upgrading from Java 8 to Java 11
  4. RPC framework design -- socket and I / 0 model
  5. RPC框架设计----I/0模型
  6. RPC framework design: I / 0 model
  7. RPC框架设计----NIO编程缓冲区Buffer
  8. RPC框架设计----NIO编程缓冲区Buffer
  9. RPC framework design -- NiO programming buffer
  10. RPC framework design -- NiO programming buffer
  11. Java多线程基础
  12. Java multithreading Foundation
  13. 码农飞升记-00-Java发展历程
  14. Development history of coder-00-java
  15. 码农飞升记-00-Java发展历程
  16. Development history of coder-00-java
  17. Spring and Autumn Moon
  18. Node.js与Spring Boot比较? - Ryan Gleason
  19. Spring WebFlux的明显陷阱 - ŁukaszKyć
  20. Spring创始人Rod大叔对YAML的真实想法
  21. Compare node.js with spring boot- Ryan Gleason
  22. Obvious pitfalls of spring Webflux- Ł ukaszKy ć
  23. Spring founder uncle rod's real thoughts on yaml
  24. 码农飞升记-02-OracleJDK是什么?OracleJDK的版本怎么选择?
  25. What is manong feisheng-02-oracle JDK? How to choose the version of Oracle JDK?
  26. Spring tide surging Xinanjiang
  27. Linux内核软中断
  28. Linux kernel soft interrupt
  29. Linux内核软中断
  30. Linux kernel soft interrupt
  31. Java multithreading Foundation
  32. The construction of Maven private library nexus
  33. I / O stream in Java
  34. JDK 16:Java 16的新功能 - InfoWorld
  35. 在Java中本地进行线程间数据传输的三种方式和源码展示
  36. jdon导致cpu 99%最后tomcat死掉---banq给予回复
  37. 用领域事件模拟AOP注入
  38. JDK 16: new function of Java 16 - InfoWorld
  39. Cartoon: from JVM lock to redis distributed lock
  40. Spring 3.1 终于加入了Cache支持
  41. Prototype与JQuery对比
  42. Three ways of data transmission between threads in Java and source code display
  43. Jdon causes 99% of CPU and Tomcat dies -- banq replies
  44. docker 原理之 user namespace(下)
  45. Simulating AOP injection with domain events
  46. Spring 3.1 finally adds cache support
  47. Comparison between prototype and jquery
  48. User namespace of docker principle (2)
  49. The way to learn java IO stream and XML
  50. Why does a seemingly correct code cause the Dubbo thread pool to be full
  51. 0 基础 Java 自学之路(2021年最新版)
  52. 0 basic Java self study road (latest version in 2021)
  53. c#—基础拾遗(1) 面向对象
  54. C - basic information (1) object oriented
  55. 技术分享|SQL和 NoSQL数据库之间的差异:MySQL(VS)MongoDB
  56. Technology sharing differences between SQL and NoSQL databases: MySQL (VS) mongodb
  57. PHP教程/面向对象-3~构造函数和析构函数
  58. Spring Cloud的Feign客户端入门
  59. 优化Spring Boot应用的Docker打包速度
  60. PHP tutorial / object oriented - 3 ~ constructor and destructor