September 2019
Intermediate to advanced
816 pages
18h 47m
English
A CompletableFuture can be explicitly completed using complete(T value), completeAsync(Supplier<? extends T> supplier), and completeAsync(Supplier<? extends T> supplier, Executor executor). T is the value returned by get(). Here it is a method that creates CompletableFuture and returns it immediately. Another thread is responsible for executing some tax computations and completing the CompletableFuture with the corresponding result:
public static CompletableFuture<Integer> taxes() { CompletableFuture<Integer> completableFuture = new CompletableFuture<>(); new Thread(() -> { int result = new Random().nextInt(100); Thread.sleep(10); completableFuture.complete(result); }).start(); return completableFuture; ...