Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to implement a multiple producer/consumer pattern in Java 6

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 ?

like image 920
Paul Taylor Avatar asked Nov 04 '22 11:11

Paul Taylor


2 Answers

Sounds like you need a java.util.concurrent.CompletionService for each stage, instead of a BlockingQueue.

like image 121
artbristol Avatar answered Nov 11 '22 19:11

artbristol


Take a look on Executors and thread pools. Here is the official tutorial: http://download.oracle.com/javase/tutorial/essential/concurrency/

like image 23
AlexR Avatar answered Nov 11 '22 19:11

AlexR