I have three Tasks:
createSimulationScenarios(), runSimulation(), and getSimulationOutput()
These tasks are done in an iterative manner:
createSimulationScenarios()creates new simulation filesrunSimulation()runs a simulation. If there are many simulations they can be executed in parallel batches.getSimulationOutput()crunches the output data as soon as a simulation is done. Can also be done in parallel as long as the specific simulation is already finished.
Then when all simulations are finished and all outputs are obtained, createSimulationScenarios() creates a new set of simulation files based on the previous batch's output. And so on...
Because each runSimulation() and each getSimulationOutput() can be done in parallel, I thought of using CompletableFuture for the implementation.
for (i = 0; i < noSimulations; ++i) {
CompletableFuture<Void> runSim = CompletableFuture.runAsync(new runSimulation(), simulationExecutor);
CompletableFuture<Void> getOutput = runSim.thenCompose(x ->
CompletableFuture.runAsync(new getSimulationOutput(), outputExecutor));
}
However, I am stuck on how to connect this chunk to another CompletableFuture for createSimulationScenarios() with the requirement that it only starts to execute when all simulations and outputs have finished (which is the start of the next iteration).
Any thoughts?