Difference between Callable and Runnable Interface

Upasana | July 25, 2020 | 2 min read | 573 views | Multithreading and Concurrency


Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.
Callable.java
public interface Callable {

   V call() throws Exception;
}

In order to convert Runnable to Callable use the following utility method provided by Executors class

Convert Runnable to Callable
Callable callable = Executors.callable(Runnable task);

Callable, however must be executed using a ExecutorService instead of Thread as shown below.

Convert Runnable to Callable
Callable<String> aCallable = () -> "dummy";
ExecutorService executorService = Executors.newSingleThreadExecutor();
final Future<String> future = executorService.submit(aCallable);
final String result = future.get();

Submitting a callable to ExecutorService returns Future Object which represents the lifecycle of a task and provides methods to check if the task has been completed or cancelled, retrieve the results and cancel the task.

Here is the source for Future Interface

java/util/concurrent/Future.java
public interface Future {
    boolean cancel(boolean mayInterruptIfRunning);

    boolean isCancelled();

    boolean isDone();

    V get() throws InterruptedException, ExecutionException;

    V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
}
What is need for both the Interfaces (callable & Runnable)?

In my opinion - In theory, the Java team could have changed the signature of the Runnable.run() method, but this would have broken binary compatibility with pre 1.5 Java code, requiring re-coding when migrating old Java code to newer JVMs. That is a BIG NO-NO. Java strives to be backwards compatible … and that’s been one of Java’s biggest selling points for business computing.

Summary of differences between Runnable and Callable

  1. A Runnable does not return a result

  2. A Runnable can’t throw checked Exception, while callable can.

  3. Runnable cannot be parametrized while Callable is a parametrized type whose type parameter indicates the return type of its run method

  4. Runnable has run() method while Callable has call() method

  5. Runnable introduced in Java 1.0 while callable was added in Java 5


Top articles in this category:
  1. Difference between Implementing Runnable and Extending Thread
  2. What is difference between HashMap and HashSet
  3. What is difference between Vector and ArrayList, which one shall be preferred
  4. Difference between HashMap, LinkedHashMap and TreeMap
  5. Difference between ExecutorService submit and execute method
  6. Comparable vs Comparator in Java
  7. Difference between HashMap and ConcurrentHashMap

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.