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

public class ParallelFuture extends FutureWork
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 Details

  • Constructor Details

    • ParallelFuture

      public ParallelFuture(String name)
      Creates a ParallelFuture to hold all child futures.
      Parameters:
      name - the name (for diagnostic data)
  • Method Details

    • setFailState

      public void setFailState(FutureState state)
      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

      public void setAbortState(FutureState state)
      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

      public FutureWork setRunnable(FutureRunnable runnable)
      Description copied from class: FutureWork
      Sets the runnable 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 class FutureWork
      Parameters:
      runnable - the code to run as part of the work
    • fail

      public void fail(String reason)
      Description copied from class: FutureWork
      Fails the work with the specified reason.
      Overrides:
      fail in class FutureWork
      Parameters:
      reason - the fail reason
    • success

      public void success()
      Description copied from class: FutureWork
      Marks the work "successful".
      Overrides:
      success in class FutureWork
    • add

      public void add(FutureWork future)
      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 class FutureWork
      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)