Callable interface in Java
Last Updated : 01 Nov, 2025
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.
- Used with ExecutorService for asynchronous or concurrent execution.
- The result of a Callable is obtained using a Future object.
- Itβs a functional interface, so you can use lambda expressions.
Syntax
public interface Callable<V> {
V call() throws Exception;
}
Here:
- V -> The type of result returned by the call() method.
- call() -> The method that performs the computation and returns a result. It can throw a checked exception.
Java import java.util.concurrent.*; public class GFG{ public static void main(String[] args) throws Exception{ // Create an ExecutorService with a single thread ExecutorService executor = Executors.newSingleThreadExecutor(); // Create a Callable task using Lambda Expression Callable<Integer> task = () ->{ System.out.println("Calculating..."); Thread.sleep(1000); return 10 * 2; }; // Submit the task and get a Future object Future<Integer> future = executor.submit(task); // Retrieve the result from the Future System.out.println("Result: " + future.get()); // Shutdown the executor executor.shutdown(); } } Output:
outputExplanation:
- A single-threaded ExecutorService is created to run a task.
- The Callable task prints "Calculating...", waits 1 second, and returns 20.
- The task is submitted using executor.submit(task), which returns a Future object.
- future.get() waits for the task to finish and retrieves the result (20).
- Finally, the executor is shut down to release resources.
Methods of Callable Interface
Although Callable has only one abstract method (call()), it works closely with the Future and ExecutorService interfaces.
- V call(): It performs the task and returns a result of type V.
- Future<V>: The Future object allows checking if the task is complete, getting the result, or canceling it.
- ExecutorService.submit(Callable<V>): Submits a Callable task for execution and returns a Future representing the pending result.
Example: Multiple Callable Tasks
Java import java.util.*; import java.util.concurrent.*; public class GFG { public static void main(String[] args) throws Exception{ ExecutorService executor = Executors.newFixedThreadPool(3); // Create multiple Callable tasks List<Callable<String> > tasks = List.of(() -> "Task 1 completed", () -> "Task 2 completed", () -> "Task 3 completed"); // Execute all tasks and get a list of Future // objects List<Future<String> > results = executor.invokeAll(tasks); // Print results for (Future<String> f : results) { System.out.println(f.get()); } executor.shutdown(); } } OutputTask 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.
Runnable vs Callable
| 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 |
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java