[英]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.