Package org.ldaptive.concurrent
Class CallableWorker<T>
- java.lang.Object
-
- org.ldaptive.concurrent.CallableWorker<T>
-
- Type Parameters:
T
- type of result from the callable
public class CallableWorker<T> extends Object
Executes callable tasks asynchronously.
-
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_NUM_THREADS
Default size of the thread pool.private ExecutorService
executorService
Executor service.protected Logger
logger
Logger for this class.
-
Constructor Summary
Constructors Constructor Description CallableWorker(String poolName)
Creates a new callable worker with a fixed sized thread pool.CallableWorker(String poolName, int numThreads)
Creates a new callable worker with a fixed sized thread pool.CallableWorker(ExecutorService es)
Creates a new callable worker.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<ExecutionException>
execute(Callable<T> callable, int count, Consumer<T> consumer)
Executes all callables and provides each result to the supplied consumer.List<ExecutionException>
execute(List<Callable<T>> callables, Consumer<T> consumer)
Executes all callables and provides each result to the supplied consumer.void
shutdown()
Shutdown the underlying executor service.
-
-
-
Field Detail
-
DEFAULT_NUM_THREADS
private static final int DEFAULT_NUM_THREADS
Default size of the thread pool.
-
logger
protected final Logger logger
Logger for this class.
-
executorService
private final ExecutorService executorService
Executor service.
-
-
Constructor Detail
-
CallableWorker
public CallableWorker(String poolName)
Creates a new callable worker with a fixed sized thread pool. The size of the thread pool is set to twice the number of available processors. SeeRuntime.availableProcessors()
.- Parameters:
poolName
- name to designate on the thread pool
-
CallableWorker
public CallableWorker(String poolName, int numThreads)
Creates a new callable worker with a fixed sized thread pool.- Parameters:
poolName
- name to designate on the thread poolnumThreads
- size of the thread pool
-
CallableWorker
public CallableWorker(ExecutorService es)
Creates a new callable worker.- Parameters:
es
- executor service to run callables
-
-
Method Detail
-
execute
public List<ExecutionException> execute(Callable<T> callable, int count, Consumer<T> consumer)
Executes all callables and provides each result to the supplied consumer.- Parameters:
callable
- callable to executecount
- number of times to execute the supplied callableconsumer
- to process callable results- Returns:
- list of exceptions thrown during the execution
-
execute
public List<ExecutionException> execute(List<Callable<T>> callables, Consumer<T> consumer)
Executes all callables and provides each result to the supplied consumer. Note that the consumer is invoked in a synchronous fashion, waiting for each result from the callables.- Parameters:
callables
- callables to executeconsumer
- to process callable results- Returns:
- list of exceptions thrown during the execution
-
shutdown
public void shutdown()
Shutdown the underlying executor service.
-
-