什么是只有一个消费者和一个生产者实现并发队列的绝对最快的方式是什么? [英] What is the absolute fastest way to implement a concurrent queue with ONLY one consumer and one producer?

查看:369
本文介绍了什么是只有一个消费者和一个生产者实现并发队列的绝对最快的方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util.concurrent.ConcurrentLinkedQueue ,但它是真的最适合这种双线程方案吗?我正在寻找在两边(生产者和消费者)可能的最小延迟。如果队列为空,您可以立即返回null,如果队列已满,您可以立即丢弃您提供的条目。

java.util.concurrent.ConcurrentLinkedQueue comes to mind, but is it really optimum for this two-thread scenario? I am looking for the minimum latency possible 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 offering.

ConcurrentLinkedQueue使用超快速和轻锁AtomicBoolean)?是否有人对ConcurrentLinkedQueue进行基准测试或了解最终最快的方式?

Does ConcurrentLinkedQueue use super fast and light locks (AtomicBoolean) ? Has anyone benchmarked ConcurrentLinkedQueue or knows about the ultimate fastest way of doing that?

其他详细信息:我认为队列应该是一个公平的,这意味着消费者不应该使消费者等待超过它所需要的时间(通过前台运行它),反之亦然。

Additional Details: I imagine the queue should be a fair one, meaning the consumer should not make the consumer wait any longer than it needs (by front-running it) and vice-versa.

推荐答案

对于我知道的Java中的最低延迟,你可以使用LMAX开发的Disruptor模式。

For the lowest latency in Java that I am aware of, you could use the Disruptor pattern developed by LMAX.

基本上,它们减少了所有的延迟,对已确立的问题的相当独特的解决方案。例如,它们尽可能预分配并重用对象(以防止垃圾收集添加额外的延迟)。

Basically, they are reducing all latency, which means a lot of rather unique solutions to well established problems. For example, they preallocate as much as possible and reuse the objects (to prevent garbage collection from adding additional latency).

他们的解决方案基于内存障碍,通过某些检查点的代码形式的执行。通过这样做,他们可以确保一个生产者线程和多个消费者线程之间的正确同步。

Their solution is based upon memory barriers, which prevent out-of-order execution of code form crossing certain checkpoints. By doing this, they can ensure proper synchronization between one producer thread and a number of consumer threads.

这是描述问题和LMAX的解决方案的白皮书和最近的YouTube视频,解释了解决方案的原理和设计细节。这将需要大量的代码重组来使用,但它是目前最快,最低的延迟,在城里的东西。

Here is a whitepaper describing the problem and LMAX's solution, and a recent youtube video explaining the rationale and design details of the solution. It would require a lot of code restructuring to use, but it's currently the fastest, lowest latency, thing in town.

这篇关于什么是只有一个消费者和一个生产者实现并发队列的绝对最快的方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆