Java 8 introduces CompletableFuture
, a new implementation of Future that is composable (includes a bunch of thenXxx methods). I\'d like to use this exclusively,
Let me suggest another (hopefully, better) option: https://github.com/vsilaev/java-async-await/tree/master/com.farata.lang.async.examples/src/main/java/com/farata/concurrent
Briefly, the idea is the following:
CompletableTask
interface -- the union of the
CompletionStage
+ RunnableFuture
ExecutorService
to return CompletableTask
from submit(...)
methods (instead of Future
)Implementation uses an alternative CompletionStage implementation (pay attention, CompletionStage rather than CompletableFuture):
Usage:
J8ExecutorService exec = J8Executors.newCachedThreadPool();
CompletionStage = exec
.submit( someCallableA )
.thenCombineAsync( exec.submit(someCallableB), (a, b) -> a + " " + b)
.thenCombine( exec.submit(someCallableC), (ab, b) -> ab + " " + c);