繁体   English   中英

获取CompletableFuture.supplyAsync的结果

[英]Getting the result of a CompletableFuture.supplyAsync

我有这段代码:

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

但出现编译错误:

类型CompletableFuture<Boolean>中的thenApply(Function<? super Boolean,? extends U>)不适用于自变量((<no type> result) -> {})

您要使用thenAccept not thenApply

thenApply花费Function是以下形式的

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

thenAccept接受以下形式的Consumer

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

您提供的lambda没有返回值; 它是无效的。 因为通用类型参数不能为空,所以您的lambda无法转换为Function接口。 另一方面, Consumer具有lambda可以满足的void返回类型。

暂无
暂无

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

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