【发布时间】:2017-06-14 09:34:45
【问题描述】:
CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> {
System.out.println("enter into completableFuture()");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("start to out of completableFuture()");
return "a";
});
System.out.println("do something else");
cf1.thenApply(v -> v + " b").thenAcceptAsync(v ->
System.out.println(v)
);
System.out.println("finalize...");
//cannot get expected result, if this line was comment out.
//TimeUnit.SECONDS.sleep(10);
代码如上。
在 jdk8 中写一个使用 CompletableFuture 的例子时,我很困惑。
我必须添加最后一行
TimeUnit.SECONDS.sleep(10);
得到预期的结果。
如果我不让其主线程休眠,我想知道程序是否已经结束。如果没有,为什么我不能得到输出?
非常感谢您的宝贵时间。
【问题讨论】:
-
谢谢,我会复习这些问题。
标签: java java-8 completable-future