我是CompletableFuture的新手, 这是一个我想尝试的简单的
CompletableFuture.supplyAsync( ()-> {System.out.println("async");});
当我尝试编译时,它给出了错误
Error:(23, 26) java: no suitable method found for supplyAsync(()->{ Syst[...]"); })
method java.util.concurrent.CompletableFuture.<U>supplyAsync(java.util.function.Supplier<U>) is not applicable
(cannot infer type-variable(s) U
(argument mismatch; bad return type in lambda expression
missing return value))
method java.util.concurrent.CompletableFuture.<U>supplyAsync(java.util.function.Supplier<U>,java.util.concurrent.Executor) is not applicable
(cannot infer type-variable(s) U
(actual and formal argument lists differ in length))
我想知道上面的问题是什么?
答案 0 :(得分:2)
就像在错误消息中所说:
缺少返回值
供应商必须返回一个值。尝试:
CompletableFuture.supplyAsync(()-> {System.out.println("async"); return null;});
或者像Flown指出的那样,使用runAsync
:
CompletableFuture.runAsync(()-> System.out.println("async"));