gpt4 book ai didi

java - CompletableFuture | thenApplyAsync 与 thenCompose 及其用例

转载 作者:行者123 更新时间:2023-12-01 14:11:02 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





CompletableFuture | thenApply vs thenCompose

(6 个回答)


4年前关闭。




我试图理解 CompletableFuture,并遇到了 2 种方法,thenApplyAsync 和 thenCompose。
我试图了解这两者之间的区别。

CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " Printing hello");
return "Hello";
}).thenCompose((String s) -> {
return CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " Adding abc");
return "abc "+s;});
}).thenApplyAsync((String s) -> {
System.out.println(Thread.currentThread().getName() + " Adding world");
return s + " World";
}).thenApplyAsync((String s) -> {
System.out.println(Thread.currentThread().getName() + " Adding name");
if (false) {
throw new RuntimeException("Oh no exception");
}
return s + " player!";
}).handle((String s, Throwable t) -> {
System.out.println(s != null ? s : "BLANK");
System.out.println(t != null ? t.getMessage() : "BLANK Exception");
return s != null ? s : t.getMessage();
});

我如何解释这些方法, thenApplyAsync将在不同的线程中执行提供的函数并返回结果,但在内部它被包装在 CompletionStage 中。而, thenCompose将返回对 CompletionStage 的引用。

那么在什么场景下,我会使用 thenComposethenApplyAsync ?

通过这个链接,但它在谈论 thenApply ,这有点不同:
CompletableFuture | thenApply vs thenCompose

最佳答案

您将使用 thenCompose当您有一个返回 CompletionStage 的操作时和 thenApply当您的操作不返回 CompletionStage 时. -> 这是在 thenApply vs thenCompose

然而Async CompletionStage 的变体界面有细微的差异和罕见的用例。

让我们考虑这个例子:

import java.util.concurrent.CompletableFuture;

public class Futures {

public static void main(String[] args) throws InterruptedException {

CompletableFuture<Void> c = CompletableFuture.runAsync(() -> {

System.out.println("run1: " + Thread.currentThread().getId());
});

c.whenComplete((r, t) -> {
System.out.println("I'm completed");
});

c.thenCompose(__ -> {
System.out.println("thenCompose1: " + Thread.currentThread().getId());
return CompletableFuture.runAsync(() -> {
System.out.println("run2: " + Thread.currentThread().getId());
});
}).thenRunAsync(() -> {
System.out.println("RunAsync1: " + Thread.currentThread().getId());
});

Thread.sleep(5000);

System.out.println("Finished");
}
}

输出类似于:
run1: 11
thenCompose1: 11
run2: 12
I'm completed
RunAsync1: 11
Finished

请注意 thenApplyAsync与影响 CompletionStage 的完成状态的非异步变体相比,不会影响原始 future 的完成状态。 .

用例:
thenCompose
您可能会 thenCompose当您有 2 个需要按顺序执行的异步操作时:
static CompletionStage<String> insert(Object database) {
throw new UnsupportedOperationException();
}

static CompletionStage<Object> get(String id) {
throw new UnsupportedOperationException();
}

public static void main(String[] args) throws InterruptedException {

Object db = new Object(); // pretend this connects to a database
CompletionStage<Object> recordInserted = insert(db).thenCompose(id -> get(id));
}

您只能在插入后检索记录。
Async thenXXXX 的变体方法

想象一下,您有一个应用程序,允许用户自行注册,并且在注册后他们将收到一封确认电子邮件以确认他们的帐户。

您不希望用户在邮件服务器宕机或编写电子邮件或执行额外检查需要很长时间时一直等待。

然后您将使用 thenApplyAsync触发发送电子邮件逻辑,因为它对您的系统并不重要。用户可以随时返回并说“再给我发送一封电子邮件”
static CompletionStage<String> register(String username) {
throw new UnsupportedOperationException();
}

static void sendConfirmationEmail(String username) {
throw new UnsupportedOperationException();
}

public static void main(String[] args) throws InterruptedException {

register("user").thenAcceptAsync(username -> sendConfirmationEmail(username));

}

在这里,您的系统将在注册完成时做出响应,但不会等待发送电子邮件,从而提高系统的响应能力。

到目前为止,我为 Async 找到的用例变种很少,但确实存在。

关于java - CompletableFuture | thenApplyAsync 与 thenCompose 及其用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46060548/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com