Search code examples
javamultithreadingconcurrencycompletable-future

Intermediate executions on CompletableFutures List


I have a list of completableFutures where each future has varying time of execution(50-300ms).

//CompletableFuture[] futures
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures);
allFutures.whenComplete(){
//Do Something
}

I want to add an intermediate step where after x ms, where I want to do some partial processing on completed futures, and combine results later

//CompletableFuture[] futures
SCHEDULER.schedule(() -> {
for(Future f: futures){
//Do Something else
}
}, 100, TimeUnit.MILLISECONDS);

CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures);
allFutures.whenComplete(){
//Do Something
}

Above doesn't look pretty to me, is there a better way of doing this, does Completable future have something out of the box for this?


Solution

  • Can be achieved through:

    CompletableFutures DelayedExecutor 
    
    or using Guava Libraries:
    
    FluentFuture.withTimeout() conversion.
    

    Both should be given an executor.