简体   繁体   English

thenAccept 和 thenApply 的区别

[英]Difference between thenAccept and thenApply

I'm reading the document on CompletableFuture and The description for thenAccept() is我正在阅读CompletableFuture上的文档, thenAccept()的描述是

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied action.返回一个新的 CompletionStage,当此阶段正常完成时,将使用此阶段的结果作为所提供操作的参数来执行。

and for thenApply() is对于thenApply()

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function.```返回一个新的 CompletionStage,当这个阶段正常完成时,将这个阶段的结果作为提供函数的参数执行。```

Can anyone explain the difference between the two with some simple examples?谁能用一些简单的例子来解释两者之间的区别?

You need to look at the full method signatures:您需要查看完整的方法签名:

CompletableFuture<Void>     thenAccept(Consumer<? super T> action)
<U> CompletableFuture<U>    thenApply(Function<? super T,? extends U> fn)

thenAccept takes a Consumer and returns a T=Void CF, ie one that does not carry a value, only the completion state. thenAccept接受一个Consumer并返回一个T=Void CF,即一个不携带值,只有完成状态的值。

thenApply on the other hand takes a Function and returns a CF carrying the return value of the function.另一方面, thenApply接受一个Function并返回一个携带函数返回值的 CF。

thenApply returns result of curent stage whereas thenAccept does not. thenApply返回当前阶段的结果,而thenAccept不返回。

Read this article: http://codeflex.co/java-multithreading-completablefuture-explained/阅读这篇文章: http : //codeflex.co/java-multithreading-completablefuture-explained/

CompletableFuture 方法

As the8472 clearly explained, they are differentiate by their output value and args and thus what you can do with them正如the8472清楚地解释的那样,它们通过输出值和参数来区分,因此你可以用它们做什么

CompletableFuture.completedFuture("FUTURE")
                    .thenApply(f -> f.toLowerCase())
                    .thenAccept(f -> System.out.println(f))
                    .thenAccept(f -> System.out.println(f))
                    .thenApply(f -> new String("FUTURE"))
                    .thenAccept(f -> System.out.println(f));
future
null
FUTURE

the Apply functions apply another function and pass a future holding a value Apply函数应用另一个函数并传递一个持有值的未来

the Accept functions consume this value and returns future holding void Accept函数消耗这个值并返回未来的持有无效

I would answer this question in the way I remember the difference between the two: Consider the following future.我会以我记得两者之间的区别的方式来回答这个问题:考虑以下未来。

CompletableFuture<String> completableFuture
  = CompletableFuture.supplyAsync(() -> "Hello");

ThenAccept basically takes a consumer and passes it the result of the computation CompletableFuture<Void> ThenAccept基本上接受一个消费者并将计算的结果传递给它CompletableFuture<Void>

CompletableFuture<Void> future = completableFuture
  .thenAccept(s -> System.out.println("Computation returned: " + s));

You can associate this with forEach in streams for easy remembering.您可以将其与streams forEach相关联以便于记忆。

Where as thenApply accepts a Function instance, uses it to process the result and returns a Future that holds a value returned by a function:thenApply接受一个Function实例时,使用它来处理结果并返回一个包含函数返回值的 Future :

CompletableFuture<String> future = completableFuture
  .thenApply(s -> s + " World");

You can associate this with map in the streams as it is actually performing a transformation.您可以将其与streams map相关联,因为它实际上正在执行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM