哪个Java阻塞队列对于单生产者单一消费者方案最有效 [英] Which Java blocking queue is most efficient for single-producer single-consumer scenarios

查看:101
本文介绍了哪个Java阻塞队列对于单生产者单一消费者方案最有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个标准的Java系统,它对我的​​生产者有严格的时序要求(1 / 100s ms事项)。

I'm working on a standard Java system with critical timing requirements for my producers (1/100s of ms matters).

我有一个生产者将东西放在一个阻塞队列中,一个消费者随后拿起那些东西并将其转储到一个文件中。消费者在数据不可用时阻止。

I have a producer placing stuff in a blocking queue, and a single consumer later picking up that stuff and dumping it to a file. The consumer blocks when data is not available.

显然,阻塞队列是合适的接口,但是如果我想最小化生产者的成本,我应该选择哪种实际实现?当我把东西放进队列时,我想尽可能少地玩锁定和分配等东西,我不介意消费者是否需要等待更长时间或者更努力地工作。

Obviously, blocking queue is the appropriate interface, but which actual implementation should I choose if I want to minimize the cost to the producer? I wan to play as little as possible on things like locking and allocating when I am putting stuff in the queue, and I don't mind if the consumer has to wait a lot longer or work a lot harder.

是否有更快的实施,因为我只有一个消费者和单个生产者?

Is there an implementation that can be faster because I only have a single consumer and single producer ?

推荐答案

嗯,确实没有太多选择。让我来看看列出的子类

Well, there really aren't too many options. Let me go through the listed subclasses:

DelayQueue LinkedBlockingDeque PriorityBlockingQueue ,以及 SynchronousQueue 全部为需要额外功能的特殊情况而制作;在这种情况下,它们没有意义。

DelayQueue, LinkedBlockingDeque, PriorityBlockingQueue, and SynchronousQueue are all made for special cases requiring extra functionality; they don't make sense in this scenario.

只留下 ArrayBlockingQueue LinkedBlockingQueue 。如果您知道如何判断是否需要 ArrayList LinkedList ,您可以自己回答这个问题。

That leaves only ArrayBlockingQueue and LinkedBlockingQueue. If you know how to tell whether you need an ArrayList or a LinkedList, you can probably answer this one yourself.

请注意,在 LinkedBlockingQueue 中,每次插入时都会动态创建链接节点;这可能会推动你走向 ArrayBlockingQueue

Note that in LinkedBlockingQueue, "linked nodes are dynamically created upon each insertion"; this might tend to push you toward ArrayBlockingQueue.

这篇关于哪个Java阻塞队列对于单生产者单一消费者方案最有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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