code: public static void run(boolean parallel, int N, LoopChunkRunner lcr, ExecutorService threadPool)
      {
          if(!parallel)
          {
              lcr.run(0, N);
              return;
          }
          
          int cores_to_use = Math.min(SystemInfo.LogicalCores, N);
          final CountDownLatch latch = new CountDownLatch(cores_to_use);
  
          IntStream.range(0, cores_to_use).forEach(threadID ->
          {
              threadPool.submit(() ->
              {
                  int start = ParallelUtils.getStartBlock(N, threadID, cores_to_use);
                  int end = ParallelUtils.getEndBlock(N, threadID, cores_to_use);
                  lcr.run(start, end);
                  latch.countDown();
              });
          });
  
          try
          {
              latch.await();
          }
          catch (InterruptedException ex)
          {
              Logger.getLogger(ParallelUtils.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
docs: This helper method provides a convenient way to break up a computation
  across <tt>N</tt> items into contiguous ranges that can be processed
  independently in parallel.
  
  @param parallel a boolean indicating if the work should be done in
  parallel. If false, it will run single-threaded. This is for code
  convenience so that only one set of code is needed to handle both cases.
  @param N the total number of items to process.
  @param lcr the runnable over a contiguous range
  @param threadPool the source of threads for the computation