Package net.imglib2.parallel
Interface TaskExecutor
-
- All Superinterfaces:
java.lang.AutoCloseable
- All Known Implementing Classes:
DefaultTaskExecutor,SequentialTaskExecutor
public interface TaskExecutor extends java.lang.AutoCloseableTaskExecutoris recommended to be used in image processing algorithms instead ofExecutorService. 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 Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidclose()<T> voidforEach(java.util.List<? extends T> parameters, java.util.function.Consumer<? super T> task)LikerunAll(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)LikeforEach(List, Consumer)but collects the results.java.util.concurrent.ExecutorServicegetExecutorService()Get the underlyingExecutorService.intgetParallelism()Get the number of threads that are used for execution.voidrunAll(java.util.List<java.lang.Runnable> tasks)This method will execute the given list of tasks.intsuggestNumberOfTasks()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.
-
-
-
Method Detail
-
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
TaskExecutorwill return 1. A multi threadedTaskExecutorwill usually return 4 times the number of threads.
-
runAll
void runAll(java.util.List<java.lang.Runnable> tasks)
This method will execute the given list of tasks. A single threadedTaskExecutorwill execute the tasks one after the other. A multi threadedTaskExecutorwill distribute the tasks to the threads. The method blocks until all tasks are completed.
-
forEach
<T> void forEach(java.util.List<? extends T> parameters, java.util.function.Consumer<? super T> task)LikerunAll(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> java.util.List<R> forEachApply(java.util.List<? extends T> parameters, java.util.function.Function<? super T,? extends R> task)LikeforEach(List, Consumer)but collects the results.
-
getExecutorService
java.util.concurrent.ExecutorService getExecutorService()
Get the underlyingExecutorService. This is not always a fully functionalExecutorService: Especially the methodsExecutorService.shutdown()andExecutorService.awaitTermination(long, TimeUnit)must not be used.
-
close
void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
-