Class ParallelFuture
java.lang.Object
com.tccc.kos.commons.util.concurrent.future.FutureWork
com.tccc.kos.commons.util.concurrent.future.ParallelFuture
- All Implemented Interfaces:
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 any and 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
boolean
void
setAbortState
(FutureState state) Sets what this future should do if a child aborts.void
setCancelOnAbort
(boolean cancelOnAbort) void
setCancelOnFail
(boolean cancelOnFail) void
setFailState
(FutureState state) Sets what this future should do if a child fails.setRunnable
(FutureRunnable runnable) Sets therunnable
to be performed by the work.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, fail, fail, getAbortAbandonedTimeoutMs, getClientData, getData, getEndState, getEstimatedEndTimeMono, getId, getName, getProgress, getReason, getReasonData, getRemainingTimeMs, getStartTimeMono, getTracker, interruptIfPending, isAbort, isCancel, isDone, isFail, isInterruptable, isInterrupted, isRunComplete, isRunStarted, isSuccess, isTerminate, prepend, remove, run, setAbortAbandonedTimeoutMs, setData, setEstimatedTimeMs, setInterruptable, setParent, setProgress, setRemainingTimeMs, setState, setState, setTimeout, setTimeout, setTimeout, toString, waitUntilFinished
-
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
-
setFailState
Sets what this future should do if a child fails. By default this future continues and can end successfully. This can be set to either ABORT or FAIL, depending on what end state is more meaningful.- Parameters:
state
- either ABORT or FAIL
-
setAbortState
Sets what this future should do if a child aborts. By default this future continues and can end successfully. This can be set to either ABORT or FAIL, depending on what end state is more meaningful.- 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
-
isCancelOnFail
public boolean isCancelOnFail() -
setCancelOnFail
public void setCancelOnFail(boolean cancelOnFail) -
isCancelOnAbort
public boolean isCancelOnAbort() -
setCancelOnAbort
public void setCancelOnAbort(boolean cancelOnAbort)
-