Search code examples
javaproducer-consumerexecutorservicecompletion-service

In Java cannot identify out when multiple producer/consumer has completed


Have a multiple producer consumer pattern producer1 - > consumer1/producer2 -> consumer2/producer 3, each producer uses a completion service but Im having problems coding the logic for working out when it has finsihed.

The problem is that main puts some (x) tasks on producers 1 queue, this in turn will cause producer 1 to put (y) tasks on producer 2 and , producer 2 will put z tasks on producer 3. x, y and z are different so main cannot just look at producer 3 completion queue and take z futures because main doesn't know z.

So I tried the poison pill idea together with a CountDownLatch initilized to 3, main knows that producer 1 only has x tasks, so I can submit a poison pill at the end , then when producer 1 recieves this it can decrement latch and send a poison pill to producer 2, and producer 2 receives one it decrements latch the send ones to producer 3. When producer 3 receives it it decrements latch. main just does countdownlatch.await() and wont be able to continue until all tasks have completed. But this only works if each executor service is limited to one thread, because when the poison pill is received by a producer it only means all preceding tasks have been started not completed .

So how do I get round this, I think I must be missing an easier solution somewhere along the line.


Solution

  • You can shutdown and awaitTermination of each executorService in turn.

    ExecutorService[] services = { executor1, executor2, executor3 };
    for(ExecutorService service: services) {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }