I need to call a method with different input parameters (like id) asynchronously, the method returns true or false, and the false output is not desirable, so I should wait for a true output. As soon as I get a true one, I do not care about the other calls, just I need to know the true output is corresponding with which input (id).
I call this piece of code, how should I know the input id of responded method? It might have a more completableFuture. The next question is in case of getting false, how can I skip and wait for getting true, because I need to recieve true from one of the myService.myMethod(int input)?
CompletableFuture<Boolean> completableFuture= CompletableFuture.supplyAsync(() -> myService.myMethod(1));
CompletableFuture<Boolean> completableFuture2= CompletableFuture.supplyAsync(() -> myService.myMethod(2));
CompletableFuture<Boolean> completableFuture3= CompletableFuture.supplyAsync(() -> myService.myMethod(3));
CompletableFuture<Boolean> completableFuture4= CompletableFuture.supplyAsync(() -> myService.myMethod(4));
CompletableFuture<Object> result =
CompletableFuture.anyOf(completableFuture, completableFuture2,completableFuture3,,completableFuture4).thenApply(s -> s);