All Downloads are FREE. Search and download functionalities are using the official Maven repository.

devutility.internal.util.concurrent.ConcurrentExecutor Maven / Gradle / Ivy

There is a newer version: 1.3.8.1
Show newest version
package devutility.internal.util.concurrent;

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;

public class ConcurrentExecutor {
	/**
	 * Run list of callables, return futures after runing completely.
	 * @param callables: Callable list
	 * @return {@code List>}
	 * @throws InterruptedException
	 */
	public static  List> run(List> callables) throws InterruptedException {
		ExecutorService executorService = ExecutorServiceUtils.threadPoolExecutor();

		if (executorService == null) {
			return new ArrayList<>();
		}

		return executorService.invokeAll(callables);
	}

	/**
	 * Run callables and return their results.
	 * @param callables: Callable list
	 * @return {@code List}
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public static  List runAndGet(List> callables) throws InterruptedException, ExecutionException {
		List list = new ArrayList<>(callables.size());
		List> futures = run(callables);

		for (Future future : futures) {
			list.add(future.get());
		}

		return list;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy