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:

 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.

Since:
1.1
Version:
1.5.1
Author:
Mikko Tommila

Constructor Summary
ParallelRunner(int numberOfProcessors)
          Create an instance of a parallel runner.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ParallelRunner

public ParallelRunner(int numberOfProcessors)
Create an instance of a parallel runner.

Parameters:
numberOfProcessors - The number of parallel threads to use.
Method Detail

lock

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

Parameters:
key - The lock key for synchronization.

unlock

public void unlock()
Finish the synchronization. The key must be first locked using the lock(Object) method.


runParallel

public void runParallel(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) method with the same key. The Runnables for processing the strides are run using the ExecutorService retrieved from ApfloatContext.getExecutorService().

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