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.Runnable
Abstract class for aRunnable
that can be run in parallel by multiple threads. Internally, theParallelRunnable
splits the work to many small batches, which are run one at a time, and can be run in parallel by multiple threads. TheParallelRunnable
isn'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.AtomicLong
completed
private long
length
private static int
MINIMUM_BATCH_SIZE
private long
preferredBatchSize
private java.util.concurrent.atomic.AtomicLong
started
-
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 anint
.protected java.lang.Runnable
getRunnable(long startValue, long length)
Get the Runnable object for strides which fit only in along
.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.
-
-
-
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:
run
in interfacejava.lang.Runnable
-
runBatch
public final boolean runBatch()
Run one batch if available. Returnstrue
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 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
-
-