public class ParallelRunner extends Object
The general paradigm for executing parallel tasks is:
ParallelRunner parallelRunner = new ParallelRunner(numberOfProcessors); parallelRunner.lock(key); try { parallelRunner.runParallel(parallelRunnable1); parallelRunner.runParallel(parallelRunnable2); parallelRunner.runParallel(parallelRunnable3); } finally { parallelRunner.unlock(); }The
key
is any object that is used to determine distinct
groups for the synchronization. For example, the shared memory lock object
from ApfloatContext.getSharedMemoryLock()
can be used as the key
to limit memory consumption when the shared memory treshold is exceeded.
To avoid any blocking due to synchronization, a ParallelRunner
can also be used without calling the lock(Object)
and unlock()
methods.
Constructor and Description |
---|
ParallelRunner(int numberOfProcessors)
Create an instance of a parallel runner.
|
Modifier and Type | Method and Description |
---|---|
void |
lock(Object key)
Start the synchronization for the given lock key.
|
void |
runParallel(ParallelRunnable parallelRunnable)
Run Runnable objects in parallel.
|
void |
unlock()
Finish the synchronization.
|
public ParallelRunner(int numberOfProcessors)
numberOfProcessors
- The number of parallel threads to use.public void lock(Object key)
unlock()
method,
in the finally
block of the immediately following
try
block, just like for concurrency locks.key
- The lock key for synchronization.public void unlock()
lock(Object)
method.public void runParallel(ParallelRunnable parallelRunnable) throws ApfloatRuntimeException
lock(Object)
method with the same key
. The Runnables
for processing the strides are run using the ExecutorService retrieved from
ApfloatContext.getExecutorService()
.parallelRunnable
- The ParallelRunnable containing the Runnable objects to be run.ApfloatRuntimeException
Copyright © 2011. All Rights Reserved.