简体   繁体   English

接下来使用CompletableFuture有什么问题?

[英]What's wrong on the following using CompletableFuture?

I am new to CompletableFuture, here is a simple one I would like to try 我是CompletableFuture的新手,这是我想尝试的简单方法

 CompletableFuture.supplyAsync( ()-> {System.out.println("async");});

When I try to compile, it gave the error 当我尝试编译时,它给出了错误

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))

I am wondering what's wrong with the above? 我想知道上面有什么问题吗?

Like it says in the error message: 如错误消息中所述:

missing return value 缺少返回值

Suppliers have to return a value. 供应商必须返回价值。 Try: 尝试:

CompletableFuture.supplyAsync(()-> {System.out.println("async"); return null;});

or, like Flown points out, use runAsync : 或者像Flown指出的那样,使用runAsync

CompletableFuture.runAsync(()-> System.out.println("async"));

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

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