简体   繁体   中英

CompletableFuture is Not waiting for child threads

I am trying to wait for processor.processFiles() to complete, the method returns void and it is an @Async method. The busy wait logic does not cause the process to wait for method to complete. Can anybody please point out what am i missing?

try{

    filesList.forEach(files -> {
        List<CompletableFuture<Void>> completableFutures  = new ArrayList<>();

        files.forEach(file-> {
            CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> 
                processor.processFiles());
            completableFutures.add(completableFuture);
        });
        while(true) {

            Thread.sleep(5000);
            boolean isComplete = completableFutures.stream().allMatch(result -> result.isDone() == true);

            if(isComplete){
                break;
            }
            LOGGER.info("processing the file...");
        }
    });
} 
catch(Exception e){

}
finally{
    closeConnections();
}

I think you've overcomplicated things.

fileList.flatMap(List::stream).parallel().forEach(file -> processor.processFiles());

The forEach will run in parallel, and return when all of the files have been processed.

At the very least, don't use side effects to populate a List .

List<CompletableFuture<Void>> completableFutures  = files.stream().map(
    file ->  CompletableFuture.runAsync(() -> processor.processFiles());
).collect( Collectors.toList());

I agree with the comment.

CompletableFuture<Void> all = CompletableFuture.allOf( completableFutures );

Then you can use get which will wait until the tasks are completed.

Another way to do this, that would skip the List + CompletableFuture.allOf and just return a single completable future.

CompletableFuture<Void> all = files.stream().map(
        file ->  CompletableFuture.runAsync(
            () -> processor.processFiles()
        )
    ).collect( 
        Collectors.reducing( 
           CompletableFuture.completedFuture(null), CompletableFuture::allOf
        )
    );

That will map file to a CompletableFuture then merge all of the resulting completable futures into a single completable future. You can call.get on it and it will return when everything is finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM