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