![]() |
VOOZH | about |
The Callable interface is a part of the java.util.concurrent package, introduced in Java 5. It represents a task that can be executed by multiple threads and return a result. Unlike the Runnable interface, Callable can return a value and throw checked exceptions.
public interface Callable<V> {
V call() throws Exception;
}
Here:
Output:
Explanation:
Although Callable has only one abstract method (call()), it works closely with the Future and ExecutorService interfaces.
Example: Multiple Callable Tasks
Task 1 completed Task 2 completed Task 3 completed
Explanation: All tasks are submitted together using invokeAll(). The method waits for all tasks to finish and then returns their results in the same order.
| Feature | Runnable | Callable |
|---|---|---|
| Return Type | Does not return any value (void run()) | Returns a value (V call()) |
| Exception Handling | Cannot throw checked exceptions | Can throw checked exceptions |
| Method Used | void run() | V call() |
| Execution Method | Executed using Thread or Executor.execute() | Submitted using ExecutorService.submit() which returns a Future |
| Use Case | Used for tasks that just need to run | Used for tasks that need to return a result |