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, the executor depends on the runtime version:

  • Java 25 or newer: virtual thread-per-task executor
  • Older Java versions: cached thread pool
  • 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