说我有这样的代码 导入java.util.concurrent.CompletableFuture;
public class Main {
public static void main(String[] args) {
CompletableFuture cf = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
System.out.println("Finished Asynctask");
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
}).thenApply(i -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Finished thenApply");
return 0;
});
System.out.println("In the main thread");
}
}
根据我对here的了解,首先将启动一个新线程,然后执行Thread.sleep(2000)
。但是主线程将继续并显示In the main thread
。然后,当supplyAsync
完成工作并打印Finished Asynctask
时,将执行thenApply
。因此输出将如下所示:
In the main thread
Finished Asynctask
Finished thenApply
但是我的输出是这样的。
In the main thread
请按我希望的方式更正代码,并进行描述以完成我的理解。