Why CompletableFuture.runAsync is not executed?

前端 未结 1 1943
后悔当初
后悔当初 2021-01-28 13:31

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.

<
相关标签:
1条回答
  • 2021-01-28 13:53

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

    The Javadoc of CompleteableFuture.runAsync() says:

    Returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool.commonPool() after it runs the given action.

    The Javadoc of ForkJoinPool.commonPool() says:

    Any program that relies on asynchronous task processing to complete before program termination should invoke commonPool().awaitQuiescence(), before exit.

    So, invoke that.

    0 讨论(0)
提交回复
热议问题