Class AsyncInferenceExecutor


  • public class AsyncInferenceExecutor
    extends Object
    AsyncInferenceExecutor provides 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 ExecutorService and allows submission of tasks as Supplier instances. Results of tasks are returned as CompletableFuture objects, 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:

    • Constructor Detail

      • AsyncInferenceExecutor

        public AsyncInferenceExecutor​(int poolSize)
        Creates a new AsyncInferenceExecutor with a fixed-size thread pool.
        Parameters:
        poolSize - the number of threads to maintain in the pool
    • Method Detail

      • 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.