LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Find the Error · Question 4

Error: passing a Callable to execute() Answer, explanation & discussion

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

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

Main.java
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newSingleThreadExecutor();
        Callable<String> job = () -> "done";
        pool.execute(job);
        pool.shutdown();
    }
}
  • A Callable cannot return String
  • B execute accepts only a Runnable; use submit(job) to run a Callable and get a Future Correct
  • C Single-thread executors cannot run Callables
  • D shutdown must be called first
Correct Answer: Option B

javac: "incompatible types: Callable cannot be converted to Runnable".

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