}
private ExperimentResults runAllRunners(
ImmutableList<ExperimentRunner> runners) {
final int threads = Math.min(numThreads, runners.size());
final ListeningExecutorService executor;
if (threads > 1) {
executor = MoreExecutors
.listeningDecorator(Executors.newFixedThreadPool(threads));
} else {
executor = MoreExecutors.sameThreadExecutor();
}
final List<SimulationResult> results;
try {
// safe cast according to javadoc
@SuppressWarnings({ "unchecked", "rawtypes" })
final List<ListenableFuture<SimulationResult>> futures = (List) executor
.invokeAll(runners);
results = Futures.allAsList(futures).get();
} catch (final InterruptedException e) {
throw new IllegalStateException(e);
} catch (final ExecutionException e) {
// FIXME need some way to gracefully handle this error. All data
// should be saved to reproduce this simulation.
throw new IllegalStateException(e);
}
executor.shutdown();
return new ExperimentResults(this, ImmutableList.copyOf(results));
}