Java Thread Executor Execute executeParallel(final List> callables, final int maxThreadCount)

Here you can find the source of executeParallel(final List> callables, final int maxThreadCount)

Description

execute Parallel

License

Apache License

Declaration

public static <T> List<T> executeParallel(final List<Callable<T>> callables, final int maxThreadCount)
            throws InterruptedException, ExecutionException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.List;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import static java.util.concurrent.Executors.newFixedThreadPool;

public class Main {
    public static <T> List<T> executeParallel(final List<Callable<T>> callables, final int maxThreadCount)
            throws InterruptedException, ExecutionException {
        final int threadCount = callables.size() > 0 && callables.size() < maxThreadCount ? callables.size()
                : maxThreadCount;//w  ww .j a v a2  s .c o  m
        ExecutorService executor = newFixedThreadPool(threadCount);
        List<T> results = new ArrayList<>();
        try {
            for (Future<T> future : executor.invokeAll(callables)) {
                results.add(future.get());
            }
        } finally {
            executor.shutdown();
        }
        return results;
    }
}

Related

  1. executeForever(final Runnable runnable)
  2. executeFuture(Callable callable)
  3. executeInCachedPool(Runnable runnable)
  4. executeInThread(Runnable r)
  5. executeInThreadPool(Runnable runnable)
  6. executePeriodicallyInThread(Runnable r, int delay, int period, TimeUnit unit)
  7. ExecuteThreads(ArrayList threads, int nThreads)
  8. invokeAsync(Runnable runnable)
  9. invokeLater(Runnable runnable)