本文介绍了为什么不执行CompletableFuture.runAsync?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为主线程必须在子线程之后结束.但是下面的代码显示了在打印异步结束"之前该过程已完成.这是什么原因?有人可以解释吗?

I consider that main thread must end after sub thread.But below code shows the process finished before print the "async end".What is the reason?Can anybody explain?Thx.

import java.util.concurrent.CompletableFuture;

public class Test {
    public static void main(String[] args) {
        CompletableFuture.runAsync(() -> {
            try {
                System.out.println("async start");
                Thread.sleep(3000);
                System.out.println("async end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println("main end");
    }
}

推荐答案

它已执行,您只是在等待结果.

It is executed, you just aren't waiting for the result.

CompleteableFuture.runAsync() 说:

ForkJoinPool.commonPool() 说:

所以,调用它.

这篇关于为什么不执行CompletableFuture.runAsync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:34
查看更多