我试图了解CompletableFutureJava 8 的工作原理。下面的代码按预期工作
CompletableFuture.supplyAsync(() -> {
System.out.println("supplyAsync Thread name " + Thread.currentThread().getName());
return "str";
}).thenApply(str -> {
System.out.println("thenApply Thread name " + Thread.currentThread().getName());
return str;
}).thenApply(str1 -> {
System.out.println("thenApply Thread name " + Thread.currentThread().getName());
return str1;
}).thenAccept(str3 -> {
System.out.println("thenAccept Thread name " + Thread.currentThread().getName());
});
System.out.println("Thread name " + Thread.currentThread().getName());
Run Code Online (Sandbox Code Playgroud)
输出:
supplyAsync Thread name ForkJoinPool.commonPool-worker-1
thenApply Thread name main
thenApply Thread name main
thenAccept Thread name main
Thread name main
Run Code Online (Sandbox Code Playgroud)
但是当我进行一些计算时,它没有按预期工作,如果我遗漏了什么,请纠正我。
CompletableFuture.supplyAsync(() -> {
System.out.println("supplyAsync Thread name …Run Code Online (Sandbox Code Playgroud)