小编Vis*_*shi的帖子

CompletableFuture 未按预期工作

我试图了解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)

java lambda asynchronous java-8 completable-future

6
推荐指数
1
解决办法
3575
查看次数

标签 统计

asynchronous ×1

completable-future ×1

java ×1

java-8 ×1

lambda ×1