Wednesday, 31 August 2016

Difference between Runnable and Callable in Java

Differences between Callable and Runnable is a frequently asked Java concurrency interview question and that is the topic of this post.

Runnable interface is around from JDK 1.0 where as Callable was added much later in Java 5 along with many other concurrent features like ConcurrentHashMap, BlockingQueue, ExecutorService.

If you see basic functionality both Callable and Runnable interfaces are implemented by any class whose instances are to be executed by another thread. Since Callable is added later so it is obvious that it would have some extra features which were not there in Runnable. Some of the added features in Callable are -

  • It can return value
  • It can throw exception

Which are not there in Runnable. These features make Callable an excellent choice if you have to run a task that involves extensive computation of a value that can be returned later.

Runnable Vs Callable

  1. Though both Callable and Runnable are interfaces and both have a single method but that method and its signature is different.

    Callable interface

    public interface Callable<V> { 
        V call() throws Exception;
    }
    

    Runnable interface

    public interface Runnable {
        public abstract void run();
    }
    
  2. Callable interface is part of the java.util.concurrent package where as Runnable interface is part of the java.lang package.
  3. If you have noticed the signature of call method in the Callable interface you can see that call method can return value
    V call() throws Exception
    
    Here V is the computed result. Callable is a generic interface and type is provided at the time of creating an instance of Callable implementation.

    As example

    Callable<Integer> callableObj = new Callable<Integer>() {
       @Override
       public Integer call() throws Exception {
        return 5;
      }
    };
    

    run() method of the Runnable interface doesn't return any value, return type for the run method is void.

  4. Another difference that can be noticed from the signatures of the call() and run() method is that you can not give a checked exception with a throws clause in run method.

    This statement will give compile time error -

    public void run() throws InterruptedException
    
    With call method checked exception can be given with throws clause.

    This statement is valid -

    public Integer call() throws InterruptedException
    
  5. In order to run a runnable task options are -
    • Thread class has a constructor that takes Runnable as parameter.
    • Executor interface has execute method which takes Runnable as parameter.
    • ExecutorService has submit method which takes Runnable as parameter.
    For Callable
    • Thread class doesn't have any constructor that takes Callable as parameter.
    • ExecutorService has submit method which takes Callable as parameter.
    • ExecutorService also has invokeAll and invokeAny methods that take Callable as parameter.
    • Executors class has callable method that can convert Runnable to Callable.
      Callable callable = Executors.callable(Runnable task); 
      

That's all for this topic Difference between Runnable and Callable in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Executor and ExecutorService in Java concurrency
  2. Callable and Future in Java concurrency
  3. Non-blocking algorithms
  4. What if run() method called directly instead of start() method
  5. Java Concurrency interview questions

You may also like -

2 comments:

  1. This is the first time I heard about Callable and you explained it so well. Thanks for sharing.

    ReplyDelete