Search code examples
javaconcurrencyexecutorservicecallable

Why Executor interface doesn't have a method, which takes Callable as a parameter?


From this answer I learned that the only difference between Callable and Runnable is that former can return a result of the execution and throw an exception.

I don't understand why Executor doesn't define a method which takes a Callable:

void execute(Callable command);

From my point of view, it'd be logical to create methods for both Runnable and Callable. In ExecutorService, which is a subinterface of Executor, there are similar submit() methods for both Runnable and Callable.

Please, explain this design decision, because I can't find any explanation on the Internet.


Solution

  • I don't understand why Executor doesn't define a method which takes a Callable.

    An Executor has a single responsibility - executing a submitted task. On this abstraction level, APIs that use only Runnables don't demand the additional features proposed by the ExecutorService.

    It'd be logical to create methods for both Runnable and Callable.

    Yes, therefore the ExecutorService interface was designed by extending the Executor. The ExecutorService offers a significant difference - providing results of task execution. That's why the Callable, TimeUnit, and lifecycle methods were added.