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);
code as above.
when writing a example of using CompletableFuture in jdk8, I got confused.
I must add the last line
TimeUnit.SECONDS.sleep(10);
to get the expected result.
I would like to know whether the program has ended if I do not let its main thread to sleep. if not, why I cannot get the output?
really thanks for you time.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…