public List<T> runDuel(final DuelExecutor<T> executor, int iterations, int numTasks) throws InterruptedException, ExecutionException { List<T> results = new ArrayList<T>(); T firstRun = executor.run(); results.add(firstRun); for (int i = 0; i < 3; i++) {} final CountDownLatch latch = new CountDownLatch(1); final AtomicLong count = new AtomicLong(iterations); List<Future<List<T>>> futures = new ArrayList<Future<List<T>>>(); for (int i = 0; i < numTasks; i++) { futures.add( pool.submit( new Callable<List<T>>() { @Override public List<T> call() throws Exception { List<T> results = new ArrayList<T>(); latch.await(); while (count.decrementAndGet() >= 0) { results.add(executor.run()); } return results; } })); } latch.countDown(); for (Future<List<T>> future : futures) { results.addAll(future.get()); } return results; }
public void duel( DuelJudge<T> judge, final DuelExecutor<T> executor, int iterations, int threadCount) throws InterruptedException, ExecutionException { T firstRun = executor.run(); List<T> runDuel = runDuel(executor, iterations, threadCount); for (T t : runDuel) { judge.judge(firstRun, t); } }