What is the purpose of the `Callable` interface?
The Callable
interface is used to represent tasks that can be executed concurrently by a thread and return a result or throw an exception. Unlike Runnable
, which cannot return a result, Callable
can return a value of a specified type and allows for exception handling.
- Usage: It’s commonly used with executor services (
ExecutorService
) for asynchronous task execution. - Method:
V call() throws Exception
— the method that defines the task, which can return a result or throw an exception.
In summary, Callable
is used for tasks that need to return a result or handle exceptions, typically in concurrent programming.