Похоже, вы не ждете, пока все Завершенные присоединятся. Запустите свой System.out.println () после присоединения к completetableFutures.
CompletableFuture.allOf(stringCompletableFuture, intCompletableFuture).join();
Также completable.get () может привести к исключению java .lang.InteruptedException и java .util.concurrent.ExecutionException. Вам необходимо обработать эти отмеченные исключения. Пример фрагмента кода
public class CompletableFutureMain {
public static void main(String[] args) {
System.out.println("Main Task : Thread Name=" + Thread.currentThread().getName());
CompletableFuture<String> stringCompletableFuture = null;
CompletableFuture<Integer> intCompletableFuture = null;
stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("Task 1 : Thread Name=" + Thread.currentThread().getName());
return getMeWelcomeMessage();
});
intCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("Task 2 : Thread Name=" + Thread.currentThread().getName());
return getMeARandomInteger();
});
CompletableFuture.allOf(stringCompletableFuture, intCompletableFuture).join();
try {
System.out.println(stringCompletableFuture.get());
System.out.println(intCompletableFuture.get().toString());
} catch (Exception exp) {
System.out.println(exp.getMessage());
}
}
private static String getMeWelcomeMessage() {
return "Trying Completable Future";
}
private static Integer getMeARandomInteger() {
return new Random().nextInt(100);
}
private static Double getMeARandomDouble() {
return new Random().nextDouble();
}
}