1: an overview

Queues are really nice data structures, ordered, regular, and buffered, just like people think of an orderly society, so in this session I’m going to talk a little bit about the common queues in Java.



2: comparison of common queues

2.1: Comparison table

The queue The data structure The border Characteristics of concurrent The characteristics of
ArrayBlockingQueue An array of Bounded (because it’s an array) blocking Simple, array structure
LinkedBlockingQueue The list Size can be specified (MaxInt by default) blocking The list
ConcurrentLinkedQueue The list unbounded Cas unlocked No lock, multiple consumers
DelayQueue The heap unbounded blocking Realize delay effect

2.2: bounded and unbounded, concurrency

  • Bounded and unbounded essentially refers to whether a queue supports unlimited stuffing, which is related to data structures. For example, an array with an array name is basically an array, and an array must specify its size, which is bounded. LinkedBlockingQueue specifies a maximum number of LinkedBlockingQueue nodes (as long as there is enough memory).

  • A concurrent queue with bloking blocks, while a concurrent queue with Concurrent uses cas optimistic locks to implement lock-free contention.

2.3: Use scenario selection

  • To predict that the queue is bounded, you can choose ArrayBlockingQueue
  • Single producer, single consumer using LinkedBlockingqueue
  • Multiple producers, single consumers use LinkedBlockingqueue
  • Single producer, multiple consumers use ConcurrentLinkedQueue
  • Multi-producer, multi-consumer ConcurrentLinkedQueue
  • Select DelayQueue if there is a delay effect

3: a pit in ConcurrentLinkedQueue

ConcurrentLinkedQuene size method please do not use !!!! It is best to use isEmpty instead, since the size method iterates through the list nodes to determine size

The source code is as follows:

4: Disruptor ring queue

Disruptor queue Disruptor queue is a next generation, very efficient, circular JVM queue used by log4j2 (see my other article on this)Disruptor queueI will not repeat it here.

5: Distributed message queues

Of course, many of our current projects or applications use distributed services on a large scale, whether redis, Dubbo, SpringCloud, etc. So many JVMS don’t have enough memory queues to meet our needs, so we need to use distributed message queues.

Here I recommend using three distributed message queues

  • Redis: Most people think redis is just a cache KV database, but redis can implement a very lightweight message queue using the listening function.

However, the message queue implemented by Redis can not be persisted, so it will be lost in case of power outages, so it is recommended that if transmitting some irrelevant buried points, such as statistics, can be used.

  • Rocketmq: a very comprehensive message queue, API friendly, recommended use, remember to pay attention to business idempotent!!
  • Kafka: Revolutionary message queue, leading the current message queue trend, high throughput, ideal for advertising, streaming processing, logging and other high throughput scenarios. But for high reliability scenarios like finance, use RocketMQ and flush simultaneously!

6:

Queue is really a kind of panacea, simple and easy to use and the use of rich scenes, can delay, can cut peak, can be asynchronous, integration of programming a lot of classic and the essence of the sentiment, is a weapon in the development process! Everyone should use more reasonable!