@Test public void testNestedParallelForLoop() throws Exception { final List<Integer> input = new LinkedList<Integer>(); for(int i = 0; i < 10; i++) { input.add(i); } final ParallelLoopResult<Integer>[] innerLoopResults = new ParallelLoopResult[input.size()]; ParallelLoopResult<Integer> loopResult = Parallel.forLoop(input, new LoopBody<Integer, Integer>() { @Override public Integer run(final Integer in1) { int sq = in1 * in1; ParallelLoopResult<Integer> innerLoopResult = Parallel.forLoop(input, new LoopBody<Integer, Integer>() { @Override public Integer run(Integer in2) { return in1 * in2; } }); innerLoopResults[in1] = innerLoopResult; return in1 * in1; } }); Assert.assertNotNull(loopResult); Assert.assertTrue(loopResult.getIsCompleted()); List<Integer> output = loopResult.getResult(); Assert.assertNotNull(output); Assert.assertEquals(input.size(), output.size()); for(int i = 0; i < input.size(); i++) { Assert.assertEquals(i * i, (int) output.get(i)); ParallelLoopResult<Integer> innerLoopResult = innerLoopResults[i]; Assert.assertNotNull(innerLoopResult); Assert.assertTrue(innerLoopResult.getIsCompleted()); List<Integer> innerOutput = innerLoopResult.getResult(); Assert.assertNotNull(innerOutput); Assert.assertEquals(input.size(), innerOutput.size()); for(int j = 0; j < input.size(); j++) { Assert.assertEquals(i*j, (int) innerOutput.get(j)); } } }
public static <T, R> ParallelLoopResult<R> forLoop( List<T> source, final LoopBody<T, R> loopBody) { if(source == null || source.isEmpty()) { return new ParallelLoopResult<>(true, Collections.<R>emptyList()); } return forLoop(source, 0, source.size(), loopBody); }
Parallel { public static <T, R> ParallelLoopResult<R> forLoop( List<T> source, final LoopBody<T, R> loopBody) { if(source == null || source.isEmpty()) { return new ParallelLoopResult<>(true, Collections.<R>emptyList()); } return forLoop(source, 0, source.size(), loopBody); } }
Parallel { public static <T, R> ParallelLoopResult<R> forLoop( List<T> source, final LoopBody<T, R> loopBody) { if(source == null || source.isEmpty()) { return new ParallelLoopResult<>(true, Collections.<R>emptyList()); } return forLoop(source, 0, source.size(), loopBody); }  }
Parallel { public static <T, R> ParallelLoopResult<R> forLoop( List<T> source, final LoopBody<T, R> loopBody) { if(source == null || source.isEmpty()) { return new ParallelLoopResult<>(true, Collections.<R>emptyList()); } return forLoop(source, 0, source.size(), loopBody); }  static ParallelLoopResult<R> forLoop(
      List<T> source,
      final LoopBody<T, R> loopBody); static ParallelLoopResult<R> forLoop(
      final List<T> source,
      int startIndex,
      int endIndex,
      final LoopBody<T, R> loopBody); }
Parallel { public static <T, R> ParallelLoopResult<R> forLoop( List<T> source, final LoopBody<T, R> loopBody) { if(source == null || source.isEmpty()) { return new ParallelLoopResult<>(true, Collections.<R>emptyList()); } return forLoop(source, 0, source.size(), loopBody); }  static ParallelLoopResult<R> forLoop(
      List<T> source,
      final LoopBody<T, R> loopBody); static ParallelLoopResult<R> forLoop(
      final List<T> source,
      int startIndex,
      int endIndex,
      final LoopBody<T, R> loopBody);  }