Package org.apfloat.internal
Class ParallelRunnable
- java.lang.Object
-
- org.apfloat.internal.ParallelRunnable
-
- All Implemented Interfaces:
java.lang.Runnable
public abstract class ParallelRunnable extends java.lang.Object implements java.lang.RunnableAbstract class for aRunnablethat can be run in parallel by multiple threads. Internally, theParallelRunnablesplits the work to many small batches, which are run one at a time, and can be run in parallel by multiple threads. TheParallelRunnableisn't completed until all batches are completed, i.e. therun()method only returns when all batches are completed.- Since:
- 1.1
- Version:
- 1.14.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicLongcompletedprivate longlengthprivate static intMINIMUM_BATCH_SIZEprivate longpreferredBatchSizeprivate java.util.concurrent.atomic.AtomicLongstarted
-
Constructor Summary
Constructors Modifier Constructor Description protectedParallelRunnable(long length)Subclass constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected longgetPreferredBatchSize()Get the preferred batch size.protected java.lang.RunnablegetRunnable(int startValue, int length)Get the Runnable object for strides which fit in anint.protected java.lang.RunnablegetRunnable(long startValue, long length)Get the Runnable object for strides which fit only in along.booleanisWorkToBeCompleted()Returns if there is some work that may be currently processed but not yet finished.booleanisWorkToBeStarted()Returns if there is still enough work left to start a new batch.voidrun()Repeatedly get a batch of work and run it, until all batches are completed.booleanrunBatch()Run one batch if available.
-
-
-
Field Detail
-
MINIMUM_BATCH_SIZE
private static final int MINIMUM_BATCH_SIZE
- See Also:
- Constant Field Values
-
length
private long length
-
preferredBatchSize
private long preferredBatchSize
-
started
private java.util.concurrent.atomic.AtomicLong started
-
completed
private java.util.concurrent.atomic.AtomicLong completed
-
-
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:
runin interfacejava.lang.Runnable
-
runBatch
public final boolean runBatch()
Run one batch if available. Returnstrueif a batch was actually acquired and run,falseif 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 anint.- 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 along.- 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
-
-