What is the absolute fastest way to implement a parallel queue with ONLY one consumer and one producer?

java.util.concurrent.ConcurrentLinkedQueue

comes to mind, but is it really optimal for this two-thread scenario? I am looking for minimum latency on both sides (producer and consumer). If the queue is empty, you can immediately return null AND if the queue is full, you can immediately discard the entry you are proposing.

Does ConcurrentLinkedQueue use super fast and lightweight locks (AtomicBoolean)? Does anyone compare ConcurrentLinkedQueue or know of the fastest way to do this?

Additional information: I believe that the queue should be fair, that is, the consumer should not force the consumer to wait longer than necessary (by starting it) and vice versa.

+3


source to share


1 answer


For the lowest latency in Java that I know of, you can use the Disruptor pattern developed by LMAX.

Basically, they reduce all latency, which means a lot of fairly unique solutions to well-known problems. For example, they predefine as much as possible and reuse objects (to prevent garbage collection from adding additional latency).



Their solution is based on memory barriers that prevent the encoding order from being violated across certain breakpoints. By doing this, they can ensure proper synchronization between one producer thread and a number of consumer threads.

Here is a white paper describing the LMAX problem and solution , and a recent youtube video explaining the rationale and detail for the solution. This will require a lot of code restructuring, but it is currently the fastest, lowest latency in town.

+4


source







All Articles