public final class Parallelization extends Object
The algorithm needs to use the TaskExecutor
returned by
getTaskExecutor()
to implement the parallelization.
Alternatively it can use getExecutorService()
.
But TaskExecutor
is simpler and better suited for image precessing algorithms.
The algorithm can be executed singleThreaded, multiThreaded or by
using a specified ExecutorService
or TaskExecutor
:
// Single-threaded call
Parallelization.runSingleThreaded( () -> myAlgorithm( image ) );
// Multi-threaded call
Parallelization.runMultiThreaded( () -> myAlgorithm( image ) );
// ExecutorService
Parallelization.withExecutor( executorService, () -> myAlgorithm( image ) );
// Multi-threaded is the default.
// A normal function call, that's not somehow wrapped by
// Parallelization.runSingleThreaded( ... ) runs multi-threaded.
myAlgorithm( image );
// Example Algorithm, that fills an image with ones.
public void myAlgorithm( RandomAccessibleInterval< IntType > image )
{
TaskExecutor taskExecutor = Parallelization.getTaskExecutor();
int numTasks = taskExecutor.suggestNumberOfTasks();
List< Interval > chunks = IntervalChunks.chunkInterval( image, numTasks );
// The TaskExecutor executes the forEach method in multiple threads, if requested.
taskExecutor.forEach( chunks, chunk -> {
for ( IntType pixel : Views.interval( image, chunk ) )
pixel.setOne();
} );
}
Modifier and Type | Method and Description |
---|---|
static ExecutorService |
getExecutorService() |
static TaskExecutor |
getTaskExecutor()
Returns the
TaskExecutor that was set for this thread. |
static <T> T |
runMultiThreaded(Callable<T> action)
To run an algorithm multi-threaded use:
|
static void |
runMultiThreaded(Runnable action)
To run an algorithm multi-threaded use:
|
static <T> T |
runSingleThreaded(Callable<T> action)
To run an algorithm single-threaded use:
|
static void |
runSingleThreaded(Runnable action)
To run an algorithm single-threaded use:
|
static <R> R |
runWithExecutor(ExecutorService executorService,
Callable<R> action)
Executes the given
Callable with the given ExecutorService ,
waits for the execution to finish and returns the result. |
static void |
runWithExecutor(ExecutorService executorService,
Runnable action)
Executes the given
Runnable with the given ExecutorService ,
and waits for the execution to finish. |
static <T> T |
runWithExecutor(TaskExecutor taskExecutor,
Callable<T> action)
Executes the given
Callable with the given TaskExecutor ,
waits for the execution to finish and returns the result. |
static void |
runWithExecutor(TaskExecutor taskExecutor,
Runnable action)
Executes the given
Runnable with the given TaskExecutor ,
and waits for the execution to finish. |
static <R> R |
runWithNumThreads(int numThreads,
Callable<R> action)
To run an algorithm a given number of threads use:
|
static void |
runWithNumThreads(int numThreads,
Runnable action)
To run an algorithm a given number of threads use:
|
public static TaskExecutor getTaskExecutor()
TaskExecutor
that was set for this thread.public static ExecutorService getExecutorService()
public static void runSingleThreaded(Runnable action)
Parallelization.runSingleThreaded( () -> myAlgorithm( input ) );
public static <T> T runSingleThreaded(Callable<T> action)
output = Parallelization.runSingleThreaded( () -> myAlgorithm( input ) );
public static void runMultiThreaded(Runnable action)
Parallelization.runMultiThreaded( () -> myAlgorithm( input ) );
public static <T> T runMultiThreaded(Callable<T> action)
output = Parallelization.runMultiThreaded( () -> myAlgorithm( input ) );
public static void runWithNumThreads(int numThreads, Runnable action)
Parallelization.runWithNumThreads( numThreads, () -> myAlgorithm( input ) );
public static <R> R runWithNumThreads(int numThreads, Callable<R> action)
output = Parallelization.runWithNumThreads( numThreads, () -> myAlgorithm( input ) );
public static void runWithExecutor(ExecutorService executorService, Runnable action)
Runnable
with the given ExecutorService
,
and waits for the execution to finish.public static <R> R runWithExecutor(ExecutorService executorService, Callable<R> action)
Callable
with the given ExecutorService
,
waits for the execution to finish and returns the result.public static void runWithExecutor(TaskExecutor taskExecutor, Runnable action)
Runnable
with the given TaskExecutor
,
and waits for the execution to finish.public static <T> T runWithExecutor(TaskExecutor taskExecutor, Callable<T> action)
Callable
with the given TaskExecutor
,
waits for the execution to finish and returns the result.Copyright © 2015–2022 ImgLib2. All rights reserved.