Sunday, 24 September 2017

Difference between the Runnable and Callable interface in Java

        This is also one of the important question of Java interview for middle level developer.  Runnable interface is added in JDK 1.0 where as Callable was added much later i.e in Java 5, along with many other concurrent features CopyOnWriteArrayList, ConcurrentHashMap, BlockingQueue and 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.  Callable interface have a some extra features which were not there in Runnable.  Those features are,
  • Callable can return value
  • It can throw exception 

Difference between Runnable and Callable:--


1)  The Callable and Runnable are interfaces and both have a single method but that method and it's signature is different.


Runnable:-

   public interface Runnable {
               public abstract void run();
     }

Callable :

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


2)  Callable interface is a part of the java.util.concurrent package whereas Runnable interface is a 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

  
     In the above code, V is the computed result.  Callable is a generic interface and type is provided at the time of creating an instance of Callable implementation.

Example:-

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

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

  @override
    public void run() {
         //does not return anything
    }
 
 
4)  Another difference that can be noticed from the signatures of the call() and run() method is that you can not give a exception with throws clause in run method.

  This below statement will give compile time error,

    public void run() throws InterruptedException
 
In call() method exception can be given with throws clause, as below.

    public Integer call() throws InterruptedException



5)  In order to run 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 a parameter.
  • ExecutorService has submit method which takes Callable as a parameter.
  • ExecutorService also has invokeAll and invokeAny methods that takes Callable as a parameter.
  • Executors class has callable method that can convert Runnable to Callable 
    Callable callable = Executors.callable(Runnable task); 



Related Posts:-
1) How many ways to create a thread in Java? Which one Prefer and why?
2) Explain Thread life cycle and difference between wait() and sleep() method
3) Deadlock in Java multithreading - Program to generate the Deadlock and to avoid Deadlock in Java
4) Thread join() method example in Java
5) Producer Consumer Problem - Solution using wait and notify In Java

2 comments:

  1. Nice post. I learn something new and challenging on sites I stumbleupon everyday. It’s always helpful to read through articles from other writers and practice a little something from their websites.

    Please Read More: Download Ebook: Ultimate Guide To Job Interview Questions Answers:

    Best rgs

    ReplyDelete
  2. I really enjoy the blog.Much thanks again. Really Great core Java online training Hyderabad

    ReplyDelete