LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Find the Error · Question 7

Error: supplyAsync with a lambda that returns nothing Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.CompletableFuture;

public class Main {
    public static void main(String[] args) {
        CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> System.out.println("loading"));
        System.out.println(f.join());
    }
}
  • A join cannot print
  • B supplyAsync needs a Supplier that returns a value, but println returns void; return a String, or use runAsync for a task with no result Correct
  • C CompletableFuture needs an executor
  • D Strings cannot be supplied asynchronously
Correct Answer: Option B

runAsync takes a Runnable and gives CompletableFuture.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links