Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.github.justincranford.threadpoolutil;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Helper class to wrap creating an ExecutorService with daemon or non-daemon thread factory.
* Default values are to use daemon threads and a pool size matching the number of available CPUs, but both can be overridden.
* Helper methods are provided to submit batch invocation requests via reflection, and optionally wait for batch results.
*/
@SuppressWarnings({"hiding","unchecked","rawtypes"})
public class ThreadPoolUtil {
/*package*/ static final Logger LOG = Logger.getLogger(ThreadPoolUtil.class.getName());
private static final String NUMBER_OF_THREADS_MUST_BE_CREATER_THAN_0 = "Number of threads must be creater than 0.";
private static final String EXECUTOR_SERVICE_MUST_NOT_BE_NULL = "Executor service must not be null.";
private static final String UNEXPECTED_EXCEPTION = "Unexpected exception";
private static final String METHOD_IS_MANDATORY = "Method is mandatory";
private static final String ARRAY_AND_ELEMENTS_OF_INSTANCES_ARRAY_ARE_MANDATORY = "Array and elements of instances array are mandatory";
private static final String ARRAY_OF_PARAMETERS_IS_MANDATORY_ELEMENTS_ARE_NOT = "Array of parameters is mandatory, elements are not";
private static final String ARRAY_AND_ELEMENTS_OF_PARAMETERS_DOUBLE_ARRAY_ARE_MANDATORY_LEAF_ELEMENTS_ARE_NOT = "Array and elements of parameters double array are mandatory, leaf elements are not";
private final ExecutorService executorService;
public ThreadPoolUtil() {
this(true, SystemUtil.SYS_AVAILABLE_CPUS);
}
public ThreadPoolUtil(final int numThreads) {
this(true, numThreads);
}
public ThreadPoolUtil(final boolean isDaemonThreadPool) {
this(isDaemonThreadPool, SystemUtil.SYS_AVAILABLE_CPUS);
}
public ThreadPoolUtil(final boolean isDaemonThreadPool, final int numThreads) {
ValidationUtil.assertGreaterThan(numThreads, 0, NUMBER_OF_THREADS_MUST_BE_CREATER_THAN_0);
this.executorService = Executors.newFixedThreadPool(numThreads, (isDaemonThreadPool ? DaemonThreadFactoryUtil.DAEMON_THREAD_FACTORY : DaemonThreadFactoryUtil.NON_DAEMON_THREAD_FACTORY));
}
public ThreadPoolUtil(final ExecutorService executorService) {
ValidationUtil.assertNonNullObject(executorService, EXECUTOR_SERVICE_MUST_NOT_BE_NULL);
this.executorService = executorService;
}
public ExecutorService getExecutorService() {
return this.executorService;
}
public List invokeAll(final List callables) throws Exception { // NOSONAR Define and throw a dedicated exception instead of using a generic one.
return ThreadPoolUtil.invokeAll(this.executorService, callables);
}
public Future