Class ParallelRunnable

java.lang.Object
org.apfloat.internal.ParallelRunnable
All Implemented Interfaces:
Runnable

public abstract class ParallelRunnable extends Object implements Runnable
Abstract class for a Runnable that can be run in parallel by multiple threads. Internally, the ParallelRunnable splits the work to many small batches, which are run one at a time, and can be run in parallel by multiple threads. The ParallelRunnable isn't completed until all batches are completed, i.e. the run() method only returns when all batches are completed.
Since:
1.1
Version:
1.14.0
  • Field Details

    • MINIMUM_BATCH_SIZE

      private static final int MINIMUM_BATCH_SIZE
      See Also:
    • length

      private long length
    • preferredBatchSize

      private long preferredBatchSize
    • started

      private AtomicLong started
    • completed

      private AtomicLong completed
  • Constructor Details

    • ParallelRunnable

      protected ParallelRunnable(long length)
      Subclass constructor.
      Parameters:
      length - The length of the work to be run.
  • Method Details

    • run

      public final void run()
      Repeatedly get a batch of work and run it, until all batches are completed. This method can (and should) be called from multiple threads in parallel.
      Specified by:
      run in interface Runnable
    • runBatch

      public final boolean runBatch()
      Run one batch if available. Returns true if a batch was actually acquired and run, false if all batches were already started and none could be run. This method can be used by any thread to steal and complete a minimal amount of work.

      Note that if a batch could not be run, it does not mean that all of the batches are already completed - some could still be running.

      Returns:
      If a batch was actually run.
    • isWorkToBeStarted

      public boolean isWorkToBeStarted()
      Returns if there is still enough work left to start a new batch.
      Returns:
      If a new batch could be started to still perform some work.
      Since:
      1.9.0
    • isWorkToBeCompleted

      public boolean isWorkToBeCompleted()
      Returns if there is some work that may be currently processed but not yet finished.
      Returns:
      If there is still some work left that is not completed yet.
      Since:
      1.9.0
    • getRunnable

      protected Runnable getRunnable(int startValue, int length)
      Get the Runnable object for strides which fit in an int.
      Parameters:
      startValue - The starting value for the stride.
      length - The length of the stride.
      Returns:
      The Runnable object for the specified stride.
    • getRunnable

      protected Runnable getRunnable(long startValue, long length)
      Get the Runnable object for strides which fit only in a long.
      Parameters:
      startValue - The starting value for the stride.
      length - The length of the stride.
      Returns:
      The Runnable object for the specified stride.
    • getPreferredBatchSize

      protected long getPreferredBatchSize()
      Get the preferred batch size.
      Returns:
      The preferred batch size.
      Since:
      1.7.0