在 Java 6 中实现多生产者/消费者模式的最佳方法
所以我有多个步骤阶段 1 ->阶段2->阶段3-> stage4 因此在某些情况下生产者将是消费者,并且在每个阶段都有多个生产者/消费者来使用多个 cpu。万一相关的一些数据包会错过步骤,即直接从阶段 1 进入阶段 4。
因此,我将为每个阶段创建一个类,与前一个阶段共享一个 BlockingQueue,但我也读到 ExecutorService 的工作方式就像一个生产者/消费者模式合二为一,所以我试图采用最好的抽象。
然而,在我看来,使用执行器,生产者位是在提交给执行器之前以顺序方式完成的,这不是我想要的。
有人可以澄清一下吗?
So I have multiple steps stage 1 -> stage 2 -> stage 3 -> stage4 so in some cases the producer would be a consumer, and at each stage there are multiple producers/consumers to make use of multiple cpus. In case relevent some packets would miss out steps, i.e go straight from stage 1 to stage 4.
So I was going to have a class for each stage, sharing a BlockingQueue with the previous stage, but Ive also read that the ExecutorService works like a Producer/Consumer pattern all in one, so Im trying to go with the best abstraction.
However it seems to me that using an Executor, that the producer bit is done before they are submitted to the executor, in a sequential way which is not what I want.
Could anyone clarify please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您每个阶段都需要一个
java.util.concurrent.CompletionService
,而不是BlockingQueue
。Sounds like you need a
java.util.concurrent.CompletionService
for each stage, instead of aBlockingQueue
.看一下执行器和线程池。这是官方教程: http://download.oracle.com/javase/tutorial/基本/并发/
Take a look on Executors and thread pools. Here is the official tutorial: http://download.oracle.com/javase/tutorial/essential/concurrency/