Class AsyncInferenceExecutor
- java.lang.Object
-
- com.codedstreams.otterstreams.sql.async.AsyncInferenceExecutor
-
public class AsyncInferenceExecutor extends Object
AsyncInferenceExecutorprovides a simple abstraction for executing tasks asynchronously using a fixed-size thread pool. It is designed for running inference or other long-running tasks concurrently without blocking the main thread.This class manages an internal
ExecutorServiceand allows submission of tasks asSupplierinstances. Results of tasks are returned asCompletableFutureobjects, enabling asynchronous processing and easy composition of multiple tasks.Typical usage:
AsyncInferenceExecutor executor = new AsyncInferenceExecutor(4); // Submit a single task CompletableFuture<ResultType> future = executor.submit(() -> performInference(input)); future.thenAccept(result -> handleResult(result)) .exceptionally(ex -> { handleError(ex); return null; }); // Submit multiple tasks and combine results List<Supplier<ResultType>> tasks = List.of( () -> performInference(input1), () -> performInference(input2), () -> performInference(input3) ); List<CompletableFuture<ResultType>> futures = tasks.stream() .map(task -> executor.submit(task) .exceptionally(ex -> { handleError(ex); // Handle exception for this task return null; // Or return a default value })) .collect(Collectors.toList()); CompletableFuture<Void> allDone = CompletableFuture.allOf( futures.toArray(new CompletableFuture[0]) ); allDone.thenRun(() -> { List<ResultType> results = futures.stream() .map(CompletableFuture::join) // join will return null for failed tasks .collect(Collectors.toList()); handleResults(results); }); executor.shutdown();Notes:
- Exceptions thrown by individual tasks can be handled using
CompletableFuture.exceptionally(java.util.function.Function<java.lang.Throwable, ? extends T>). - Using
CompletableFuture.join()will propagate unchecked exceptions; returning a default value inexceptionallyis a safe way to avoid stopping the whole batch. - The
shutdown()method should be called when no further tasks need to be submitted.
- Exceptions thrown by individual tasks can be handled using
-
-
Constructor Summary
Constructors Constructor Description AsyncInferenceExecutor(int poolSize)Creates a newAsyncInferenceExecutorwith a fixed-size thread pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidshutdown()Shuts down the executor, preventing new tasks from being submitted.<T> CompletableFuture<T>submit(Supplier<T> task)Submits a task for asynchronous execution.
-
-
-
Method Detail
-
submit
public <T> CompletableFuture<T> submit(Supplier<T> task)
Submits a task for asynchronous execution.The task is executed on the internal thread pool, and the result is returned as a
CompletableFuture. Any exception thrown during execution will be captured by theCompletableFutureand can be handled usingCompletableFuture.exceptionally(java.util.function.Function).- Type Parameters:
T- the type of the result produced by the task- Parameters:
task- aSupplierrepresenting the task to execute- Returns:
- a
CompletableFuturerepresenting the pending completion of the task
-
shutdown
public void shutdown()
Shuts down the executor, preventing new tasks from being submitted.Previously submitted tasks continue to execute. Use this method when you no longer need to submit any tasks to the executor. For immediate shutdown of running tasks, consider using
ExecutorService.shutdownNow()on a custom implementation.
-
-