简体   繁体   English

获取CompletableFuture.supplyAsync的结果

[英]Getting the result of a CompletableFuture.supplyAsync

I have this piece of code: 我有这段代码:

CompletableFuture
    .supplyAsync(() -> {
        return  smsService.sendSMS(number);
    }).thenApply(result -> {
        LOG.info("SMS sended " + result);
    });

but I got a compilation error: 但出现编译错误:

The method thenApply(Function<? super Boolean,? extends U>) in the type CompletableFuture<Boolean> is not applicable for the arguments ((<no type> result) -> {}) 类型CompletableFuture<Boolean>中的thenApply(Function<? super Boolean,? extends U>)不适用于自变量((<no type> result) -> {})

You want to use thenAccept not thenApply 您要使用thenAccept not thenApply

thenApply takes a Function which is of the form thenApply花费Function是以下形式的

public interface Function<T, R> {
    R apply(T t);
}

thenAccept takes a Consumer which is of the form thenAccept接受以下形式的Consumer

public interface Consumer<T> {
    void accept(T t);
}

The lambda you provided does not have a return value; 您提供的lambda没有返回值; it is void. 它是无效的。 Because a generic type parameter cannot be void your lambda cannot be casted as a Function interface. 因为通用类型参数不能为空,所以您的lambda无法转换为Function接口。 On the other hand, the Consumer has a void return type which the lambda could satisfy. 另一方面, Consumer具有lambda可以满足的void返回类型。

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

相关问题 使用 Mockito 测试 CompletableFuture.supplyAsync - Testing CompletableFuture.supplyAsync with Mockito ForkJoinPool在CompletableFuture.supplyAsync()中的行为 - Behaviour of ForkJoinPool in CompletableFuture.supplyAsync() CompletableFuture.supplyAsync() 没有 Lambda - CompletableFuture.supplyAsync() without Lambda CompletableFuture.supplyAsync与新的CompletableFuture() - CompletableFuture.supplyAsync vs new CompletableFuture() 简单的 CompletableFuture.supplyAsync() 导致 IllegalMonitorStateException 错误 - Simple CompletableFuture.supplyAsync() leads to IllegalMonitorStateException error 使用CompletableFuture.supplyAsync返回多个值 - Returning multiple values with CompletableFuture.supplyAsync CompletableFuture.supplyAsync 代码的代码覆盖率 - code coverage of CompletableFuture.supplyAsync code 如何将CompletableFuture.supplyAsync与PriorityBlockingQueue一起使用? - How do I use CompletableFuture.supplyAsync together with PriorityBlockingQueue? JDK8 CompletableFuture.supplyAsync 如何处理interruptedException - JDK8 CompletableFuture.supplyAsync how to deal with interruptedException 如何将completableFuture.supplyAsync()的返回类型分配给对象? - how to assign the return type of completableFuture.supplyAsync() to the object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM