Package net.imglib2.parallel
Class DefaultTaskExecutor
- java.lang.Object
-
- net.imglib2.parallel.DefaultTaskExecutor
-
- All Implemented Interfaces:
java.lang.AutoCloseable,TaskExecutor
public class DefaultTaskExecutor extends java.lang.Object implements TaskExecutor
ATaskExecutorthat wraps around a givenExecutorService.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ExecutorServiceexecutorService
-
Constructor Summary
Constructors Constructor Description DefaultTaskExecutor(java.util.concurrent.ExecutorService executorService)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()private <T> T[]concatenate(T[] a, T[] b)<T> voidforEach(java.util.List<? extends T> parameters, java.util.function.Consumer<? super T> task)LikeTaskExecutor.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)LikeTaskExecutor.forEach(List, Consumer)but collects the results.java.util.concurrent.ExecutorServicegetExecutorService()Get the underlyingExecutorService.intgetParallelism()Get the number of threads that are used for execution.private voidinvokeAllIgnoreResults(java.util.List<java.util.concurrent.Callable<java.lang.Object>> callables)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.private java.lang.RuntimeExceptionunwrapExecutionException(java.lang.Throwable e)ExecutorServicewrap all exceptions thrown by any task into aExecutionException, this makes the stack traces rather hard to read.
-
-
-
Method Detail
-
getExecutorService
public java.util.concurrent.ExecutorService getExecutorService()
Description copied from interface:TaskExecutorGet the underlyingExecutorService. This is not always a fully functionalExecutorService: Especially the methodsExecutorService.shutdown()andExecutorService.awaitTermination(long, TimeUnit)must not be used.- Specified by:
getExecutorServicein interfaceTaskExecutor
-
getParallelism
public int getParallelism()
Description copied from interface:TaskExecutorGet the number of threads that are used for execution.- Specified by:
getParallelismin interfaceTaskExecutor
-
runAll
public void runAll(java.util.List<java.lang.Runnable> tasks)
Description copied from interface:TaskExecutorThis 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.- Specified by:
runAllin interfaceTaskExecutor
-
suggestNumberOfTasks
public int suggestNumberOfTasks()
Description copied from interface:TaskExecutorIf 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.- Specified by:
suggestNumberOfTasksin interfaceTaskExecutor
-
forEach
public <T> void forEach(java.util.List<? extends T> parameters, java.util.function.Consumer<? super T> task)Description copied from interface:TaskExecutorLikeTaskExecutor.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:
forEachin interfaceTaskExecutor
-
forEachApply
public <T,R> java.util.List<R> forEachApply(java.util.List<? extends T> parameters, java.util.function.Function<? super T,? extends R> task)Description copied from interface:TaskExecutorLikeTaskExecutor.forEach(List, Consumer)but collects the results.- Specified by:
forEachApplyin interfaceTaskExecutor
-
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)
ExecutorServicewrap all exceptions thrown by any task into aExecutionException, this makes the stack traces rather hard to read. This method unwraps theExecutionExceptionand 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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceTaskExecutor
-
-