DEV Community

Aswin Arya
Aswin Arya

Posted on

Difference Between Runnable and Callable in Java

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();
Enter fullscreen mode Exit fullscreen mode

Example

Runnable task = () -> {
    System.out.println("Runnable task executed");
};
Enter fullscreen mode Exit fullscreen mode

🔹 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;
Enter fullscreen mode Exit fullscreen mode

Example

Callable<Integer> task = () -> {
    return 100;
};
Enter fullscreen mode Exit fullscreen mode

🔹 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)