Class ParallelRunnable

  • All Implemented Interfaces:
    java.lang.Runnable

    public abstract class ParallelRunnable
    extends java.lang.Object
    implements java.lang.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
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ParallelRunnable​(long length)
      Subclass constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected long getPreferredBatchSize()
      Get the preferred batch size.
      protected java.lang.Runnable getRunnable​(int startValue, int length)
      Get the Runnable object for strides which fit in an int.
      protected java.lang.Runnable getRunnable​(long startValue, long length)
      Get the Runnable object for strides which fit only in a long.
      boolean isWorkToBeCompleted()
      Returns if there is some work that may be currently processed but not yet finished.
      boolean isWorkToBeStarted()
      Returns if there is still enough work left to start a new batch.
      void run()
      Repeatedly get a batch of work and run it, until all batches are completed.
      boolean runBatch()
      Run one batch if available.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • length

        private long length
      • preferredBatchSize

        private long preferredBatchSize
      • started

        private java.util.concurrent.atomic.AtomicLong started
      • completed

        private java.util.concurrent.atomic.AtomicLong completed
    • Constructor Detail

      • ParallelRunnable

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

      • 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 java.lang.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 java.lang.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 java.lang.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