public class ParallelUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
OPTIMAL_THREADS
Indicates to the createThreadPool() method to estimate the "optimal"
number of threads for the computer currently executing the codes.
|
Modifier | Constructor and Description |
---|---|
protected |
ParallelUtil()
Protected constructor since this is a utility class.
|
Modifier and Type | Method and Description |
---|---|
static <ResultType> |
compareTimes(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks)
Compares the times needed by running the tasks sequentially versus
parallel.
|
static <ResultType> |
compareTimes(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks,
java.util.concurrent.ThreadPoolExecutor threadPool)
Compares the times needed by running the tasks sequentially versus
parallel.
|
static java.util.concurrent.ThreadPoolExecutor |
createThreadPool()
Creates a thread pool with the "optimal" number of threads.
|
static java.util.concurrent.ThreadPoolExecutor |
createThreadPool(int numRequestedThreads)
Creates a thread pool with the given number of threads.
|
static java.util.concurrent.ThreadFactory |
defaultDaemonThreadFactory()
Creates a version of the default thread factory from
Executors.defaultThreadFactory() that creates daemon threads. |
static <ResultType> |
executeInParallel(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks)
Executes the given Callable tasks in parallel using a default thread
pool
|
static <ResultType> |
executeInParallel(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks,
ParallelAlgorithm algorithm)
Executes the given Callable tasks in parallel using a given thread
pool
|
static <ResultType> |
executeInParallel(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks,
java.util.concurrent.ThreadPoolExecutor threadPool)
Executes the given Callable tasks in parallel using a given thread
pool
|
static <ResultType> |
executeInSequence(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks)
Executes the given Callable tasks sequentially in series.
|
static int |
getDefaultNumThreads()
Gets the current default number of threads to use when called using the
default createThreadPool() method
|
static int |
getNumThreads(ParallelAlgorithm algorithm)
Gets the number of threads in a ParallelAlgorithm by querying the
thread pool
|
static int |
getNumThreads(java.util.concurrent.ThreadPoolExecutor threadPool)
Gets the number of threads in a ThreadPoolExecutor
|
static void |
setDefaultNumThreads(int defaultNumThreads)
Sets the current default number of threads to use when calling the
default createThreadPool() method
|
public static final int OPTIMAL_THREADS
protected ParallelUtil()
public static int getDefaultNumThreads()
public static void setDefaultNumThreads(int defaultNumThreads)
defaultNumThreads
- Number of threads to use during a default call to createThreadPool()public static java.util.concurrent.ThreadPoolExecutor createThreadPool()
public static java.util.concurrent.ThreadPoolExecutor createThreadPool(int numRequestedThreads)
numRequestedThreads
- Number of threads to create, or OPTIMAL_THREADS an "optimal" number
of threads for the current computer.public static int getNumThreads(ParallelAlgorithm algorithm)
algorithm
- ParallelAlgorithm to considerpublic static int getNumThreads(java.util.concurrent.ThreadPoolExecutor threadPool)
threadPool
- ThreadPoolExecutor to considerpublic static <ResultType> java.util.ArrayList<ResultType> executeInParallel(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
ResultType
- Type of results returned by the Callable tasks.tasks
- Callable tasks to be split across multiple coresjava.lang.InterruptedException
- If interruptedjava.util.concurrent.ExecutionException
- If the Callable task can't execute its methodpublic static <ResultType> java.util.ArrayList<ResultType> executeInParallel(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks, java.util.concurrent.ThreadPoolExecutor threadPool) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
ResultType
- Type of results returned by the Callable tasks.tasks
- Callable tasks to be split across multiple coresthreadPool
- Thread pool to use for the parallelizationjava.lang.InterruptedException
- If interruptedjava.util.concurrent.ExecutionException
- If the Callable task can't execute its methodpublic static <ResultType> java.util.ArrayList<ResultType> executeInParallel(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks, ParallelAlgorithm algorithm) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
ResultType
- Type of results returned by the Callable tasks.tasks
- Callable tasks to be split across multiple coresalgorithm
- Uses the embedded thread pool in the ParallelAlgorithmjava.lang.InterruptedException
- If interruptedjava.util.concurrent.ExecutionException
- If the Callable task can't execute its methodpublic static <ResultType> java.util.ArrayList<ResultType> executeInSequence(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks) throws java.lang.Exception
ResultType
- Type of results returned by the Callable tasks.tasks
- Callable tasks to execute sequentiallyjava.lang.Exception
- If one of the Callables can't execute its method.public static <ResultType> NamedValue<java.lang.Double> compareTimes(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks)
ResultType
- Result types of the Callable taskstasks
- Callable tasks to execute in parallel or sequentiallypublic static <ResultType> NamedValue<java.lang.Double> compareTimes(java.util.Collection<? extends java.util.concurrent.Callable<ResultType>> tasks, java.util.concurrent.ThreadPoolExecutor threadPool)
ResultType
- Result types of the Callable taskstasks
- Callable tasks to execute in parallel or sequentiallythreadPool
- Thread pool to use for the parallelizationpublic static java.util.concurrent.ThreadFactory defaultDaemonThreadFactory()
Executors.defaultThreadFactory()
that creates daemon threads.
Daemon threads mean that the application can exit even if the threads
are still alive.