Class CallableWorker<T>

  • Type Parameters:
    T - type of result from the callable

    public class CallableWorker<T>
    extends Object
    Executes callable tasks asynchronously.
    • 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. See Runtime.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 pool
        numThreads - 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 execute
        count - number of times to execute the supplied callable
        consumer - 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 execute
        consumer - to process callable results
        Returns:
        list of exceptions thrown during the execution
      • shutdown

        public void shutdown()
        Shutdown the underlying executor service.