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

5 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. Those guidelines additionally worked to become a good way to
    recognize that other people online have the identical fervor like mine
    to grasp great deal more around this condition.I have one doubt why java become more popular then all lavguag

    java training in chennai


    java training in bangalore


    ReplyDelete
  3. Ciitnoida provides Core and java training institute in

    noida
    . We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-

    oriented, java training in noida , class-based build

    of Java has made it one of most popular programming languages and the demand of professionals with certification in Advance Java training is at an

    all-time high not just in India but foreign countries too.

    By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13

    years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best

    Java training in Noida.

    java training institute in noida
    java training in noida

    ReplyDelete
  4. Best Summer Internship In Noida

    These technologies prepare individuals for fields like software programming, technical support, graphic design, software testing, business analytics. Embedded Systems, Industrial Automation Training and more. Candidates go through a series of comprehensive practical sessions where they work on live problems and implement solutions on real-time basis.
    It has a dedicated placement cell which provides 100% placement assistance to students. The benefits that a student gets out of summer training are innumerable. CIITN,Best Summer training Center for B.Tech/CS/CSE/IT/ BCA/MCA/ B.E /M.tech / B.sc/ M.sc/ Engineering Student has already accomplished itself successfully in the field of Training and Development after setting milestones and bringing smiles to the faces of more than 1 Lakh students. CIITN was incorporated in the year 2002 and over the years CIITN has grown remarkably into the greatest training giant of Northern India.
    There is no looking back to technology! Those passionate for it need not worry about the professional prospects it offers. Courses and trainings in the field of Computer Science and Applications open a wide array of choices for individuals. All you need to do it prepare yourself right!

    Summer Internship Course In Noida

    Summer Internship Training In Noida

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

    ReplyDelete