org.apfloat.internal
Class ParallelRunner

java.lang.Object
  extended by org.apfloat.internal.ParallelRunner

public class ParallelRunner
extends Object

Class for running Runnable objects in parallel using multiple threads.

The general paradigm for executing parallel tasks is:

 lock(key, numberOfProcessors);
 try
 {
     runParallel(key, parallelRunnable1);
     runParallel(key, parallelRunnable2);
     runParallel(key, parallelRunnable3);
 }
 finally
 {
     unlock(key);
 }
 
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 to limit memory consumption when the shared memory treshold is exceeded. To avoid any blocking due to synchronization, a new Object() can be used as the key.

Since:
1.1
Version:
1.5
Author:
Mikko Tommila

Method Summary
static void lock(Object key, int numberOfProcessors)
          Start the synchronization for the given lock key.
static void runParallel(Object key, ParallelRunnable parallelRunnable)
          Run Runnable objects in parallel.
static void unlock(Object key)
          Finish the synchronization for the given lock key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

lock

public static void lock(Object key,
                        int numberOfProcessors)
Start the synchronization for the given lock key. The key must be released using the unlock(Object) method, in the finally block of the immediately following try block, just like for concurrency locks.

Parameters:
key - The lock key for synchronization.
numberOfProcessors - The number of parallel threads to contribute to the parallel processing.

unlock

public static void unlock(Object key)
Finish the synchronization for the given lock key. The key must be first locked using the lock(Object,int) method.

Parameters:
key - The lock key for synchronization.

runParallel

public static void runParallel(Object key,
                               ParallelRunnable parallelRunnable)
                        throws ApfloatRuntimeException
Run Runnable objects in parallel. The whole length of the ParallelRunnable is split to multiple, small strides. The strides are run in parallel using multiple threads. The number of threads is the total number of threads contributed by the concurrent calls to the lock(Object,int) method with the same key. The Runnables for processing the strides are run using the ExecutorService retrieved from ApfloatContext.getExecutorService().

Parameters:
key - The lock key for synchronization.
parallelRunnable - The ParallelRunnable containing the Runnable objects to be run.
Throws:
ApfloatRuntimeException