【问题标题】:Main Thread is not waiting for the Worker Thread to finish in Completable Future主线程没有等待工作线程在 Completable Future 中完成
【发布时间】:2020-08-04 03:06:28
【问题描述】:

//主线程在 Completable Future 中没有等待 Worker 线程完成。当我在 Completables 上运行 System.out.println 时,它们没有按顺序运行。

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


}

【问题讨论】:

  • 为什么要按顺序运行?您已经启动了两个异步操作。这意味着不存在排序关系。等待与它有什么关系?当你在等车时,火车会改变它的时刻表吗?

标签: multithreading java-8 parallel-processing


【解决方案1】:

看起来您并没有等待所有 Completables 加入。 加入 completableFutures 后运行 System.out.println()。

CompletableFuture.allOf(stringCompletableFuture, intCompletableFuture).join();

completable.get() 也可能导致 java.lang.InteruptedException 和 java.util.concurrent.ExecutionException。您需要处理这些 Checked Exceptions。 示例代码片段

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


}

【讨论】:

  • 由于get() 确实在等待结果,因此无需执行allOf(…).join()。这种多余的等待对结果没有影响。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多