Threads and lifecycle question 6 of 180 junior
What is the difference between Runnable and Callable, and where does Future fit?
Callableis faster because it does not allocate- Correct:
Callablereturns a value and may throw a checked exception;Runnabledoes neither Runnablecan only be used with raw threadsFutureis only usable withRunnable
Runnable.run() returns void and cannot throw checked exceptions, so a failure inside it has nowhere
to go except the thread's uncaught exception handler. Callable.call() returns V and may throw,
which is what lets an executor capture the outcome.
submit wraps either into a Future. Calling get() blocks until the task finishes and then either
returns the value or throws ExecutionException with the original failure as its cause. That is the
important half: with execute(Runnable) an exception is printed and lost, with submit it is stored
in the Future and is lost only if nobody ever calls get().