Основной поток не ждет, пока рабочий поток завершит sh в Completable Future - PullRequest
1 голос
/ 04 августа 2020

// Основной поток не ждет, пока рабочий поток завершит sh в Completable Future. Когда я запускаю System.out.println на Completables, они не работают в порядке.

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();
    });

        System.out.println(stringCompletableFuture.get());
        System.out.println(intCompletableFuture.get().toString());


}

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();
}


}

1 Ответ

1 голос
/ 04 августа 2020

Похоже, вы не ждете, пока все Завершенные присоединятся. Запустите свой 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();
}


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...