### Question:
ExecUtils { public static <T,R> R collectInParallel(int numTasks, int parallelism, boolean privatePool, Function<Integer,T> task, Collector<? super T,?,R> collector) { Preconditions.checkArgument(numTasks >= 1); Preconditions.checkArgument(parallelism >= 1); Objects.requireNonNull(task); Objects.requireNonNull(collector); IntStream range = IntStream.range(0, numTasks); IntStream taskIndices = parallelism > 1 ? range.parallel() : range; if (parallelism > 1 && privatePool) { ForkJoinPool pool = new ForkJoinPool(parallelism); try { return pool.submit(() -> taskIndices.mapToObj(task::apply).collect(collector)).get(); } catch (InterruptedException e) { throw new IllegalStateException(e); } catch (ExecutionException e) { throw new IllegalStateException(e.getCause()); } finally { pool.shutdown(); } } else { return taskIndices.mapToObj(task::apply).collect(collector); } } private  ExecUtils(); static void doInParallel(int numTasks, Consumer<Integer> task); static void doInParallel(int numTasks,
                                  int parallelism,
                                  boolean privatePool,
                                  Consumer<Integer> task); static R collectInParallel(int numTasks,
                                          int parallelism,
                                          boolean privatePool,
                                          Function<Integer,T> task,
                                          Collector<? super T,?,R> collector);  }### Answer:
@Test public void testCollectInParallel() { assertEquals(1, maxActiveCollect(1, 1, false)); assertEquals(1, maxActiveCollect(1, 1, true)); assertEquals(1, maxActiveCollect(4, 1, false)); assertEquals(1, maxActiveCollect(4, 1, true)); int cores = Runtime.getRuntime().availableProcessors(); assertEquals(Math.min(cores, 4), maxActiveCollect(4, 4, false)); assertEquals(4, maxActiveCollect(4, 4, true)); }