Class TimeboxedExecutor
java.lang.Object
io.fluxzero.common.TimeboxedExecutor
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> TcallAndWait(@NonNull Callable<T> task, Duration maxDuration) Executes the given task within the specified maximum duration.<T> TcallAndWait(Callable<T> task, Duration maxDuration, Supplier<? extends T> fallback) Executes the given task within the specified maximum duration.voidclose()booleanrunAndWait(ThrowingRunnable task, Duration maxDuration) Executes the given task and waits for completion up to the given maximum duration.booleanrunAndWaitSafely(ThrowingRunnable task, Duration maxDuration) Executes the given task and waits for completion up to the given maximum duration.booleanrunAndWaitSafely(ThrowingRunnable task, Duration maxDuration, boolean logFailure) Executes the given task and waits for completion up to the given maximum duration.
-
Constructor Details
-
TimeboxedExecutor
public TimeboxedExecutor()
-
-
Method Details
-
runAndWaitSafely
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 executemaxDuration- the maximum duration to wait- Returns:
trueif the task completed in time,falseif it timed out
-
runAndWaitSafely
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 executemaxDuration- the maximum duration to wait- Returns:
trueif the task completed in time,falseif it timed out
-
runAndWait
Executes the given task and waits for completion up to the given maximum duration.- Parameters:
task- the task to executemaxDuration- the maximum duration to wait- Returns:
trueif the task completed in time,falseif it timed out
-
callAndWait
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 executemaxDuration- the maximum duration to waitfallback- 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 executemaxDuration- 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:
closein interfaceAutoCloseable
-