Class ParallelFuture
java.lang.Object
com.tccc.kos.commons.util.concurrent.future.FutureWork
com.tccc.kos.commons.util.concurrent.future.ParallelFuture
- All Implemented Interfaces:
Abortable
,Cancelable
,Terminable
,Runnable
Allows a collection of individual futures to be run in parallel, where the
overall state of
ParallelFuture
reflects the result of the child
futures. All child futures are executed in the scheduler thread pool to ensure
clean decoupling from event threads that may be used to finalize the state of
the future.
Cancelling this future will cancel all running children.
The end state of this future is SUCCESS if all children are SUCCESS.
The end state in case of FAIL or ABORT can be set to achieve the desired outcome.
- Since:
- 1.0
- Version:
- 2022-05-05
-
Field Summary
FieldsFields inherited from class com.tccc.kos.commons.util.concurrent.future.FutureWork
REASON_errStartCallbackFailed, REASON_errTimedOut
-
Constructor Summary
ConstructorsConstructorDescriptionParallelFuture
(String name) Creates aParallelFuture
to hold all child futures. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(FutureWork future) Adds a future to the collection.void
Fails the work with the specified reason.long
When running in parallel, the estimated completion time is equal to the longest child task.boolean
If true, cancel all child futures if any individual child future aborts.boolean
If true, cancel all child futures if any individual child future fails.void
setAbortState
(FutureState state) Sets the end state to use for the parent future if any child future aborts.void
setFailState
(FutureState state) Sets the end state to use for the parent future if any child future fails.setRunnable
(FutureRunnable runnable) Sets therunnable
to be performed by the work.void
setTermianteOnAbort
(boolean enable) If true, cancel all child futures if any individual child future aborts, otherwise ignore failed children and allow all other children to complete.void
setTerminatelOnFail
(boolean enable) If true, cancel all child futures if any individual child future fails, otherwise ignore failed children and allow all other children to complete.void
success()
Marks the work "successful".Methods inherited from class com.tccc.kos.commons.util.concurrent.future.FutureWork
abort, abort, abort, append, cancel, cancel, cancel, cancelTimeout, disableAbortAbandoned, execute, fail, fail, getAbortAbandonedTimeoutMs, getClientData, getData, getEndState, getEstimatedEndTimeMono, getId, getName, getNote, getProgress, getReason, getReasonData, getRemainingTimeMs, getRootFuture, getStartTimeMono, getTracker, interruptIfPending, isAbort, isCancel, isDone, isFail, isInterruptable, isInterrupted, isRunComplete, isRunStarted, isSuccess, isTerminate, prepend, remove, run, setAbortAbandonedTimeoutMs, setData, setEstimatedTimeMs, setInterruptable, setNote, setParent, setProgress, setRemainingTimeMs, setState, setState, setTimeout, setTimeout, setTimeout, toString, waitUntilFinished, whenFinished
-
Field Details
-
REASON_errAbortedSibling
- See Also:
-
REASON_errFailedSibling
- See Also:
-
-
Constructor Details
-
ParallelFuture
Creates aParallelFuture
to hold all child futures.- Parameters:
name
- the name (for diagnostic data)
-
-
Method Details
-
isTerminateOnFail
public boolean isTerminateOnFail()If true, cancel all child futures if any individual child future fails. -
setTerminatelOnFail
public void setTerminatelOnFail(boolean enable) If true, cancel all child futures if any individual child future fails, otherwise ignore failed children and allow all other children to complete.- Parameters:
enable
- true to cancel children if any child fails
-
isTerminateOnAbort
public boolean isTerminateOnAbort()If true, cancel all child futures if any individual child future aborts. -
setTermianteOnAbort
public void setTermianteOnAbort(boolean enable) If true, cancel all child futures if any individual child future aborts, otherwise ignore failed children and allow all other children to complete.- Parameters:
enable
- true to cancel children if any child aborts
-
setFailState
Sets the end state to use for the parent future if any child future fails. By default the end state will beFAIL
but it can be set toABORT
. This does not impact whether the children continue to run, just what the final end state will be.- Parameters:
state
- either ABORT or FAIL
-
setAbortState
Sets the end state to use for the parent future if any child future aborts. By default the end state will beABORT
but it can be set toFAIL
. This does not impact whether the children continue to run, just what the final end state will be.- Parameters:
state
- either abort or fail
-
setRunnable
Description copied from class:FutureWork
Sets therunnable
to be performed by the work. Unlike Java futures, it is possible for this work to complete without changing the end state. For example, this work may simply kick off a hardware process and some other mechanism is used to determine when that hardware is done, and that will change the state.This
runnable
can set the state, but callbacks won't be triggered until this function returns. This prevents race conditions between this code and any state event callbacks.If this runnable throws an exception, then the state is set to "aborted".
- Overrides:
setRunnable
in classFutureWork
- Parameters:
runnable
- the code to run as part of the work
-
fail
Description copied from class:FutureWork
Fails the work with the specified reason.- Overrides:
fail
in classFutureWork
- Parameters:
reason
- the fail reason
-
success
public void success()Description copied from class:FutureWork
Marks the work "successful".- Overrides:
success
in classFutureWork
-
add
Adds a future to the collection. Futures can only be added before processing starts.- Parameters:
future
- the work to add to the sequence
-
getEstimatedTimeMs
public long getEstimatedTimeMs()When running in parallel, the estimated completion time is equal to the longest child task.- Overrides:
getEstimatedTimeMs
in classFutureWork
- Returns:
- the estimated time of the operation
-