Class TimeboxedExecutor

java.lang.Object
io.fluxzero.common.TimeboxedExecutor
All Implemented Interfaces:
AutoCloseable

public final class TimeboxedExecutor extends Object implements AutoCloseable
Utility for running tasks with a maximum execution duration.

If a task does not complete within the configured duration, it is cancelled using interruption.

By default, each task runs on its own named virtual thread. A caller-supplied executor retains its execution policy. Closing this utility shuts down its executor without waiting for active tasks to finish.

  • Constructor Details

    • TimeboxedExecutor

      public TimeboxedExecutor()
  • Method Details

    • runAndWaitSafely

      public boolean runAndWaitSafely(ThrowingRunnable task, Duration maxDuration)
      Executes the given task and waits for completion up to the given maximum duration.

      If the task fails, false is returned without logging the failure.

      Parameters:
      task - the task to execute
      maxDuration - the maximum duration to wait
      Returns:
      true if the task completed in time, false if it timed out
    • runAndWaitSafely

      public boolean runAndWaitSafely(ThrowingRunnable task, Duration maxDuration, boolean logFailure)
      Executes the given task and waits for completion up to the given maximum duration.

      If the task fails, a warning is optionally logged and false is returned.

      Parameters:
      task - the task to execute
      maxDuration - the maximum duration to wait
      Returns:
      true if the task completed in time, false if it timed out
    • runAndWait

      public boolean runAndWait(ThrowingRunnable task, Duration maxDuration)
      Executes the given task and waits for completion up to the given maximum duration.
      Parameters:
      task - the task to execute
      maxDuration - the maximum duration to wait
      Returns:
      true if the task completed in time, false if it timed out
    • callAndWait

      public <T> T callAndWait(Callable<T> task, Duration maxDuration, Supplier<? extends T> fallback)
      Executes the given task within the specified maximum duration. If the task times out, the fallback value is returned.
      Type Parameters:
      T - the task result type
      Parameters:
      task - the task to execute
      maxDuration - the maximum duration to wait
      fallback - supplies the fallback value in case of timeout
      Returns:
      the task result, or the fallback result if the task timed out
    • callAndWait

      public <T> T callAndWait(@NonNull @NonNull Callable<T> task, Duration maxDuration) throws TimeoutException
      Executes the given task within the specified maximum duration.
      Type Parameters:
      T - the task result type
      Parameters:
      task - the task to execute
      maxDuration - the maximum duration to wait
      Returns:
      the task result
      Throws:
      TimeoutException - if the task does not complete within the given duration
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable