public <T>List<T> runTasks(final List<Callable<T>> tasks,final long timeout,final TimeUnit unit,final int maxParallel) throws InterruptedException, ExecutionException, TimeoutException {
  if (tasks == null)   throw new IllegalArgumentException();
  if (unit == null)   throw new IllegalArgumentException();
  if (maxParallel <= 0)   throw new IllegalArgumentException();
  if (tasks.isEmpty()) {
    return Collections.emptyList();
  }
  if (tasks.size() == 1) {
    return runOne(tasks.get(0),timeout,unit);
  }
 else   if (maxParallel == Integer.MAX_VALUE) {
    return runMaxParallel(tasks,timeout,unit);
  }
 else   if (maxParallel > 1) {
    return runLimitedParallel(tasks,timeout,unit,maxParallel);
  }
 else {
    return runSequence(tasks,timeout,unit);
  }
}

public < T > List < T > runTasks ( final List < Callable < T > > tasks , final long timeout , final TimeUnit unit , final int maxParallel ) throws InterruptedException , ExecutionException , TimeoutException { if ( tasks == null ) throw new IllegalArgumentException ( ) ; if ( unit == null ) throw new IllegalArgumentException ( ) ; if ( maxParallel <= 0 ) throw new IllegalArgumentException ( ) ; if ( tasks . isEmpty ( ) ) { return Collections . emptyList ( ) ; } if ( tasks . size ( ) == 1 ) { return runOne ( tasks . get ( 0 ) , timeout , unit ) ; } else if ( maxParallel == Integer . MAX_VALUE ) { return runMaxParallel ( tasks , timeout , unit ) ; } else if ( maxParallel > 1 ) { return runLimitedParallel ( tasks , timeout , unit , maxParallel ) ; } else { return runSequence ( tasks , timeout , unit ) ; } }
runs a set of tasks , checking their futures . when maxparallel is gt one ( 1 ) , tasks are run in chunks of up to maxparallel tasks at once . new chunks are not submitted until all tasks in the existing chunk are complete . higher concurrency limited parallelism may be obtained with a purpose specific thread pool of a given worker thread capacity , but there is overhead associated with creating the thread pool and its worker threads .
