java – CompletableFuture |然后应用vs thenCompose
|
我不能让我的头围绕thenApply()和thenCompose()之间的区别. 那么有人可以提供一个有效的用例吗? 从Java文档: thenApply(Function<? super T,? extends U> fn)
thenCompose(Function<? super T,? extends CompletionStage<U>> fn)
我得到thenCompon的第二个参数扩展了CompletionStage,然后应用不会. 有人可以提供一个例子,在这种情况下,我必须使用然后应用,当thenCompose? 解决方法那么如果你有一个同步映射函数,那么应用它.CompletableFuture<Integer> future =
CompletableFuture.supplyAsync(() -> 1)
.thenApply(x -> x+1);
那么如果你有一个异步映射函数(即返回一个CompletableFuture的函数),那么使用该函数.然后它将直接返回结果,而不是嵌套的未来. CompletableFuture<Integer> future =
CompletableFuture.supplyAsync(() -> 1)
.thenCompose(x -> CompletableFuture.supplyAsync(() -> x+1)); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
