Class DefaultTaskExecutor

  • All Implemented Interfaces:
    java.lang.AutoCloseable, TaskExecutor

    public class DefaultTaskExecutor
    extends java.lang.Object
    implements TaskExecutor
    A TaskExecutor that wraps around a given ExecutorService.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.ExecutorService executorService  
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultTaskExecutor​(java.util.concurrent.ExecutorService executorService)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      private <T> T[] concatenate​(T[] a, T[] b)  
      <T> void forEach​(java.util.List<? extends T> parameters, java.util.function.Consumer<? super T> task)
      Like TaskExecutor.runAll(List) but - instead of a list of tasks - it takes a list of parameters and a function that is called for each of the parameters.
      <T,​R>
      java.util.List<R>
      forEachApply​(java.util.List<? extends T> parameters, java.util.function.Function<? super T,​? extends R> task)
      Like TaskExecutor.forEach(List, Consumer) but collects the results.
      java.util.concurrent.ExecutorService getExecutorService()
      Get the underlying ExecutorService.
      int getParallelism()
      Get the number of threads that are used for execution.
      private void invokeAllIgnoreResults​(java.util.List<java.util.concurrent.Callable<java.lang.Object>> callables)  
      void runAll​(java.util.List<java.lang.Runnable> tasks)
      This method will execute the given list of tasks.
      int suggestNumberOfTasks()
      If there is a big task that could be split into sub tasks for parallelization, this method gives you a reasonable number of sub tasks.
      private java.lang.RuntimeException unwrapExecutionException​(java.lang.Throwable e)
      ExecutorService wrap all exceptions thrown by any task into a ExecutionException, this makes the stack traces rather hard to read.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • executorService

        private final java.util.concurrent.ExecutorService executorService
    • Constructor Detail

      • DefaultTaskExecutor

        public DefaultTaskExecutor​(java.util.concurrent.ExecutorService executorService)
    • Method Detail

      • getExecutorService

        public java.util.concurrent.ExecutorService getExecutorService()
        Description copied from interface: TaskExecutor
        Get the underlying ExecutorService. This is not always a fully functional ExecutorService: Especially the methods ExecutorService.shutdown() and ExecutorService.awaitTermination(long, TimeUnit) must not be used.
        Specified by:
        getExecutorService in interface TaskExecutor
      • getParallelism

        public int getParallelism()
        Description copied from interface: TaskExecutor
        Get the number of threads that are used for execution.
        Specified by:
        getParallelism in interface TaskExecutor
      • runAll

        public void runAll​(java.util.List<java.lang.Runnable> tasks)
        Description copied from interface: TaskExecutor
        This method will execute the given list of tasks. A single threaded TaskExecutor will execute the tasks one after the other. A multi threaded TaskExecutor will distribute the tasks to the threads. The method blocks until all tasks are completed.
        Specified by:
        runAll in interface TaskExecutor
      • suggestNumberOfTasks

        public int suggestNumberOfTasks()
        Description copied from interface: TaskExecutor
        If there is a big task that could be split into sub tasks for parallelization, this method gives you a reasonable number of sub tasks.

        A single threaded TaskExecutor will return 1. A multi threaded TaskExecutor will usually return 4 times the number of threads.

        Specified by:
        suggestNumberOfTasks in interface TaskExecutor
      • forEach

        public <T> void forEach​(java.util.List<? extends T> parameters,
                                java.util.function.Consumer<? super T> task)
        Description copied from interface: TaskExecutor
        Like TaskExecutor.runAll(List) but - instead of a list of tasks - it takes a list of parameters and a function that is called for each of the parameters.
        Specified by:
        forEach in interface TaskExecutor
      • invokeAllIgnoreResults

        private void invokeAllIgnoreResults​(java.util.List<java.util.concurrent.Callable<java.lang.Object>> callables)
      • unwrapExecutionException

        private java.lang.RuntimeException unwrapExecutionException​(java.lang.Throwable e)
        ExecutorService wrap all exceptions thrown by any task into a ExecutionException, this makes the stack traces rather hard to read. This method unwraps the ExecutionException and thereby reveals the original exception, and ensures it's complete stack trace.
      • concatenate

        private <T> T[] concatenate​(T[] a,
                                    T[] b)
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface TaskExecutor