Both Runnable and Callable are interfaces used to create tasks that run in separate threads.
However, Callable is an advanced version introduced to overcome the limitations of Runnable.
🔹 Runnable Interface
- Present since Java 1.0
- Used to execute a task in a thread
- Cannot return any result
- Cannot throw checked exceptions
Method
public void run();
Example
Runnable task = () -> {
System.out.println("Runnable task executed");
};
🔹 Callable Interface
- Introduced in Java 5 (
java.util.concurrent) - Can return a result
- Can throw checked exceptions
- Works with ExecutorService
Method
public V call() throws Exception;
Example
Callable<Integer> task = () -> {
return 100;
};
🔹 Key Differences
| Feature | Runnable | Callable |
|---|---|---|
| Package | java.lang | java.util.concurrent |
| Method | run() | call() |
| Return Value | ❌ No | ✅ Yes |
| Checked Exceptions | ❌ Not allowed | ✅ Allowed |
| Used With | Thread / execute() | ExecutorService / submit() |
| Result Handling | Not possible | Uses Future |
🔹 When to Use?
✅ Use Runnable when:
- No result is required
- Simple background task
✅ Use Callable when:
- Result is needed
- Exception handling required
- Parallel computations
🚀 Promotional Content
Build strong multithreading and concurrency skills with real industry examples in No 1 Java Real Time Projects Online Training in ammerpet.
Top comments (0)