Question 7
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
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!