jOOL icon indicating copy to clipboard operation
jOOL copied to clipboard

java.util.concurrent Helpers for joining Futures using Tuples

Open billoneil opened this issue 9 years ago • 4 comments

Possibly create a way to join and map CompletableFutures.

public static <T1, T2, T3> Tuple3<T1, T2, T3> joinAll(
        CompletableFuture<T1> f1,
        CompletableFuture<T2> f2,
        CompletableFuture<T3> f3) {
    // Do we even need this allOf since we join below anyway.
    CompletableFuture.allOf(f1, f2, f3).join();

    return Tuple.tuple(f1.join(), f2.join(), f3.join());
}

Could be combined like so. return Futures.joinAll(emailFuture, tagFuture, rolesFuture).map(User::new);

Full example shown here https://gist.github.com/billoneil/f949def19560f379bec9aa640b19120c#file-gistfile1-java-L23

billoneil avatar Feb 11 '17 15:02 billoneil

Thank you very much for your suggestion. That is an interesting approach. Should we decide to provide utilities for the JDK's concurrency APIs (see also https://github.com/jOOQ/jOOL/issues/283), this multi-join-into-tuple approach definitely makes sense.

lukaseder avatar Feb 11 '17 17:02 lukaseder

@lukaseder any thoughts on this since we added the new CompletionStage? Ideally it would accept CompletionStages but they would need to be converted to CompletableFuture to call join() or CompletableFuture.allOf(

billoneil avatar May 04 '17 02:05 billoneil

Thanks for pinging, @billoneil. I think this idea needs to ripe a bit, first. Improving this area seems to be scratching an itch that few people have, and at this stage, I'm very uncertain of what a good API would be. I currently don't want to rush adding this new API.

lukaseder avatar May 08 '17 09:05 lukaseder

No problem totally agree. Just wanted to see if you had any thoughts. I might make something similar internally and see if I like it.

billoneil avatar May 08 '17 14:05 billoneil