我是新來CompletableFuture, 這裏是一個簡單的,我想嘗試以下使用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))
我想知道什麼是錯的以上?
那麼你不返回任何東西 - 什麼樣的「供應商」你以爲你傳遞? –
您缺少返回https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#supplyAsync-java.util.function.Supplier- –
您應該使用'CompletableFuture .runAsync(() - > System.out.println(「async」));'而是。 – Flown