Paralellizer { public List<Integer> doALotOfWork(List<String> workItems) { List<Integer> results = new ArrayList<Integer>(); List<Future<Integer>> asyncResults = new ArrayList<Future<Integer>>(); System.out.println("Parent: Starting all tasks to run in parallel..."); for (String workItem : workItems) { Future<Integer> asyncResult = worker.executeWorkItem(workItem); asyncResults.add(asyncResult); } System.out.println("Parent: Started all tasks. Blocking to collect results..."); for (Future<Integer> asyncResult : asyncResults) { try { results.add(asyncResult.get()); } catch (Exception e) { throw new RuntimeException(e); } } System.out.println("Parent:Collected all results. Parallel processing done"); return results; }  List<Integer> doALotOfWork(List<String> workItems);  }
@Test public void execute20TasksInParallelLastsLessThan5TasksLength() { List<String> items = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z"); long t0 = System.currentTimeMillis(); paralellizer.doALotOfWork(items); assertTrue("Lasted less than 5 seconds", System.currentTimeMillis() - t0 < 5000); System.out.println("Test OK"); }