Interface TaskExecutor

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
DefaultTaskExecutor, SequentialTaskExecutor

public interface TaskExecutor extends AutoCloseable
TaskExecutor is recommended to be used in image processing algorithms instead of ExecutorService. It's simpler to use, and allows single threaded execution.



// Example of a multi threaded method that fills an image with ones.
public void fillWithOnes( RandomAccessibleInterval< IntType > image, TaskExecutor taskExecutor )
{
    int numTasks = taskExecutor.suggestNumberOfTasks();
    List< RandomAccessibleInterval< IntType > > chunks = splitImageIntoChunks( image, numTasks );

    // The TaskExecutor executes the forEach method in multi threads, if requested.
    taskExecutor.forEach( chunks, chunk -> {
        for ( IntType pixel : Views.iterable( chunk ) )
            pixel.setOne();
    } );
}

// The method can be run multi threaded or single threaded
fillWithOnes( image, TaskExecutors.singleThreaded() );
fillWithOnes( image, TaskExecutors.multiThreaded() );

  • Method Details

    • getParallelism

      int getParallelism()
      Get the number of threads that are used for execution.
    • suggestNumberOfTasks

      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.

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

    • runAll

      void runAll(List<Runnable> tasks)
      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.
    • forEach

      <T> void forEach(List<? extends T> parameters, Consumer<? super T> task)
      Like 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.
    • forEachApply

      <T,R> List<R> forEachApply(List<? extends T> parameters, Function<? super T, ? extends R> task)
      Like forEach(List, Consumer) but collects the results.
    • getExecutorService

      ExecutorService getExecutorService()
      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.
    • close

      void close()
      Specified by:
      close in interface AutoCloseable