In Java multithreading, tasks are usually executed using threads. The Runnable interface is commonly used to create tasks, but it has a limitation — it cannot return a result and cannot throw checked exceptions.
To overcome this limitation, Java introduced Callable and Future in the java.util.concurrent package.
These two concepts are widely used in concurrent programming and asynchronous task execution.
What is Callable?
Callable is an interface that represents a task that returns a result and can throw exceptions.
It is similar to Runnable, but with two key differences:
- It returns a value
- It can throw checked exceptions
Callable Interface Syntax
public interface Callable<V> {
V call() throws Exception;
}
The call() method is executed by a thread and returns a result.
Example of Callable
import java.util.concurrent.Callable;
class MyTask implements Callable<Integer> {
public Integer call() {
return 10 + 20;
}
}
Here the call() method returns 30.
What is Future?
Future is an interface that represents the result of an asynchronous computation.
When a Callable task is submitted to an ExecutorService, the result is returned as a Future object.
The Future object allows you to:
- Check if the task is completed
- Retrieve the result
- Cancel the task
Example Using Callable and Future
import java.util.concurrent.*;
public class FutureExample {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Callable<Integer> task = () -> {
return 50 + 50;
};
Future<Integer> result = executor.submit(task);
System.out.println("Result: " + result.get());
executor.shutdown();
}
}
Output
Result: 100
Explanation:
- A Callable task is created.
- It is submitted to ExecutorService.
- The Future object stores the result.
-
future.get()retrieves the result.
Important Methods of Future
get()
Returns the result of the computation.
result.get();
isDone()
Checks if the task is completed.
result.isDone();
cancel()
Cancels the task.
result.cancel(true);
isCancelled()
Checks whether the task was cancelled.
result.isCancelled();
Difference Between Runnable and Callable
| Feature | Runnable | Callable |
|---|---|---|
| Package | java.lang | java.util.concurrent |
| Return Value | No return value | Returns a value |
| Method | run() | call() |
| Exception Handling | Cannot throw checked exceptions | Can throw exceptions |
| Used With | Thread | ExecutorService |
Real-World Use Cases
Callable and Future are commonly used in:
- Parallel data processing
- Web server request handling
- Background tasks
- Microservices communication
- High-performance Java applications
They are especially useful when multiple tasks run in parallel and results need to be collected later.
Promotional Content
If you want to master advanced Java concepts like Multithreading, Concurrency, JVM Internals, and System Design, practical training is essential.
Join the Top System Design with Java Online Training program to learn how modern large-scale Java systems are designed and optimized.
This training program covers:
- System Design Fundamentals
- Java Concurrency and Multithreading
- Scalable Backend Architecture
- Microservices Design Patterns
- Real-world system design case studies
Top comments (0)