public class ParallelExecutor extends Object
Caution: Avoid using another executor within parallelized calls, this would create numerous threads, leading to much memory consumption and huge overhead for thread switching. It is recommended to parallelize the outermost-scoped tasks.
| Constructor and Description |
|---|
ParallelExecutor()
Creates an instance using default concurrency number, which is the
number of available processors returned by
Runtime.getRuntime().availableProcessors()
|
ParallelExecutor(int concurrency)
Creates an instance with a specified concurrency number.
|
ParallelExecutor(String executorName)
Creates an instance with a specified executor name.
|
ParallelExecutor(String executorName,
int concurrency)
Creates an instance with a specified concurrency number, and a name of
the executor.
|
| Modifier and Type | Method and Description |
|---|---|
<T> void |
conditionalForEach(boolean conditionToParallelize,
Iterable<T> iterable,
IterationBody<T> body)
|
void |
conditionalForLoop(boolean conditionToParallelize,
int start,
int end,
int increment,
LoopBody body)
Runs a parallel for-loop only if
conditionToParallelize is true. |
void |
conditionalForLoop(boolean conditionToParallelize,
int start,
int end,
LoopBody body)
Calls
conditionalForLoop
with increment of 1. |
<T> List<T> |
executeAll(Callable<T>... tasks)
Executes an arbitrary number of
Callable tasks, and returns a
list of results in the same order. |
<T> List<T> |
executeAll(List<? extends Callable<T>> tasks)
Executes a list of
Callable tasks, and returns a list of results
in the same sequential order as tasks. |
<T> T |
executeAny(Callable<T>... tasks)
Executes a list of tasks in parallel, and returns the result from the
earliest successfully completed tasks (without throwing an exception).
|
<T> T |
executeAny(List<? extends Callable<T>> tasks)
Executes a list of tasks in parallel, and returns the result from the
earliest successfully completed tasks (without throwing an exception).
|
<T> void |
forEach(Iterable<T> iterable,
IterationBody<T> body)
Runs a "foreach" loop in parallel.
|
void |
forLoop(int start,
int end,
int increment,
LoopBody body)
Runs a for-loop in parallel.
|
void |
forLoop(int start,
int end,
LoopBody body)
Calls
forLoop
with increment of 1. |
int |
getConcurrency()
Returns the number of threads used for parallel execution.
|
static ParallelExecutor |
getSharedInstance()
Gets the globally shared executor.
|
boolean |
isWorkerThread()
Tests membership in this executor without depending on thread names.
|
void |
shutdown()
Shuts down the executor gracefully.
|
public ParallelExecutor()
Runtime.getRuntime().availableProcessors()
public ParallelExecutor(String executorName)
executorName - the executor name (for debug purpose)public ParallelExecutor(int concurrency)
concurrency - the maximum number of threads can be used when executing a list of taskspublic ParallelExecutor(String executorName, int concurrency)
executorName - the executor name (for debug purpose)concurrency - the maximum number of threads can be used when executing a list of taskspublic static ParallelExecutor getSharedInstance()
public void shutdown()
public int getConcurrency()
public boolean isWorkerThread()
public <T> List<T> executeAll(List<? extends Callable<T>> tasks) throws MultipleExecutionException
Callable tasks, and returns a list of results
in the same sequential order as tasks.T - the type of resultstasks - the list of tasksMultipleExecutionException - if one or more tasks throws an exceptionpublic <T> List<T> executeAll(Callable<T>... tasks) throws MultipleExecutionException
Callable tasks, and returns a
list of results in the same order. This is a convenient method and is the
same as calling:
executeAll(Arrays.<Callable<T>>asList(tasks));
T - the type of resultstasks - the list of tasksMultipleExecutionException - if one or more tasks throws an exceptionpublic <T> T executeAny(List<? extends Callable<T>> tasks) throws ExecutionException
T - the type of resultstasks - the list of tasksExecutionException - if no task successfully completespublic <T> T executeAny(Callable<T>... tasks) throws ExecutionException
executeAny(Arrays.<Callable<T>>asList(tasks));
T - the type of resultstasks - the list of tasksExecutionException - if no task successfully completespublic void forLoop(int start,
int end,
LoopBody body)
throws MultipleExecutionException
forLoop
with increment of 1.start - the first loop index (inclusive)end - the last loop index (exclusive)body - the loop bodyMultipleExecutionException - if one or more partitioned for-loop throws an exceptionpublic void forLoop(int start,
int end,
int increment,
LoopBody body)
throws MultipleExecutionException
for (int i = start; i < end; i += increment) {
body.run(i);
}
start - the first loop index (inclusive)end - the last loop index (exclusive)increment - the increment of the loop index in each iterationbody - the loop bodyMultipleExecutionException - if one or more partitioned for-loop throws an exceptionpublic void conditionalForLoop(boolean conditionToParallelize,
int start,
int end,
int increment,
LoopBody body)
throws MultipleExecutionException
conditionToParallelize is true.
Otherwise, or when called from this executor's own worker, the loop body
runs on the calling thread to avoid thread-starvation deadlock.conditionToParallelize - the condition to parallelize the for-loop executionstart - the first loop index (inclusive)end - the last loop index (exclusive)increment - the increment of the loop index in each iterationbody - the loop bodyMultipleExecutionException - if one or more partitioned for-loop throws an exceptionpublic void conditionalForLoop(boolean conditionToParallelize,
int start,
int end,
LoopBody body)
throws MultipleExecutionException
conditionalForLoop
with increment of 1.conditionToParallelize - the condition to parallelize the for-loop executionstart - the first loop index (inclusive)end - the last loop index (exclusive)body - the loop bodyMultipleExecutionException - if one or more partitioned for-loop throws an exceptionpublic <T> void forEach(Iterable<T> iterable, IterationBody<T> body) throws MultipleExecutionException
for (T item : collection) {
body.run(item);
}
T - data type of elements in iterableiterable - the Iterable collectionbody - the loop bodyMultipleExecutionException - if one or more threads throws an exceptionpublic <T> void conditionalForEach(boolean conditionToParallelize,
Iterable<T> iterable,
IterationBody<T> body)
throws MultipleExecutionException
forEach
only if conditionToParallelize is true.
Otherwise, or when called from this executor's own worker, the loop body
runs on the calling thread to avoid thread-starvation deadlock.T - data type of elements in iterableconditionToParallelize - the condition to parallelize the "foreach" executioniterable - the Iterable collectionbody - the loop bodyMultipleExecutionException - if one or more threads throws an exceptionCopyright © 2010-2026 NM Group. All Rights Reserved.