public class ParallelExecutor
extends Object
Typical usage example:
ParallelExecutor executor = new ParallelExecutor(maxNumberOfThreads); for (Callable task : tasks) { executor.addTask(task); } try { executor.waitUntilDone(); } catch (Exception ex) { Logger.log(Logger.SEVERE, executor, Resource.ERROR_GENERAL.getValue(), ex); } executor.terminate();
Modifier and Type | Class and Description |
---|---|
static class |
ParallelExecutor.ParallelExecutorThreadFactory
A factory for creating ParallelExecutorThread objects.
|
Constructor and Description |
---|
ParallelExecutor(ExecutorService threadPool,
String name)
Instantiates a new parallel executor.
|
ParallelExecutor(int nThreads,
String name)
Instantiates a new ParallelExecutor using nThreads as a max possible
number of threads.
|
Modifier and Type | Method and Description |
---|---|
Future<?> |
addTask(Callable<?> task)
Adds the task to the pool and schedule it execution.
|
Future<?> |
addTask(Callable<?> task,
boolean ignoreException)
Adds the task to the pool and schedule it execution.
|
List<Future<?>> |
getResults()
Gets the results for each task.
|
boolean |
isTerminated()
Checks if execution was terminated.
|
void |
terminate()
Terminates execution of all active tasks.
|
void |
waitUntilDone()
Waits until all tasks are finished or execution is terminated.
|
public ParallelExecutor(ExecutorService threadPool, String name)
threadPool
- the thread poolname
- the namepublic ParallelExecutor(int nThreads, String name)
nThreads
- the max possible number of threadsname
- the namepublic Future<?> addTask(Callable<?> task)
task
- the taskpublic Future<?> addTask(Callable<?> task, boolean ignoreException)
ignoreException == true
any exception during execution of
the task will be ignored.task
- the taskignoreException
- the ignore exception flagpublic List<Future<?>> getResults()
public boolean isTerminated()
public void terminate()
public void waitUntilDone() throws Exception
Exception
- in case of any errorCopyright © 2010-2020 Toolsverse. All Rights Reserved.