What is difference between Callable and Runnable Interface?

Carvia Tech | May 05, 2019 | 2 min read | 181 views | Multithreading and Concurrency


As per Java documentation

"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


Buy my ebook for complete question bank

Most of these questions has been answered in my eBook "Cracking the Core Java Interview" updated on June 2018, that you can buy from this link:

Buy from Shunya (DRM Free PDF download with updates)

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 Comparable and Comparator in Java
  5. What is difference between sleep() and wait() method in Java?
  6. What is difference between JDK JRE and JVM
  7. Difference between HashMap and ConcurrentHashMap



Find more on this topic:
Core Java image
Core Java

Core Java - OOP Concepts, Garbage Collection, Multi-threading, Collections Framework, Java 8 Features, Lambda Functions, Streams.

Last updated 1 month ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info