What is the difference between `Runnable` and `Callable`?
The key differences between Runnable
and Callable
are:
- Return Value:
Runnable
: Does not return a result or throw an exception. Itsrun()
method isvoid
.Callable
: Returns a result and can throw exceptions. Itscall()
method returns a value of typeV
.
- Exception Handling:
Runnable
: Cannot throw checked exceptions in therun()
method.Callable
: Can throw checked exceptions in thecall()
method.
- Use Case:
Runnable
is used for tasks that do not require a result and do not throw checked exceptions.Callable
is used for tasks that need to return a result or handle exceptions.
In summary, use Runnable
for simple tasks without a return value, and use Callable
for tasks that need a result or may throw exceptions.