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.
I don't understand why
Executor
doesn't define a method which takes aCallable
.
An Executor
has a single responsibility - executing a submitted task. On this abstraction level, APIs that use only Runnable
s don't demand the additional features proposed by the ExecutorService
.
It'd be logical to create methods for both
Runnable
andCallable
.
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.