Tuesday, July 6, 2021

Difference Between Runnable And Callable in Java

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

Runnable and Callable interfaces in Java

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 in Java 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 checked exception.

Which are not there in Runnable interface in Java. 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 in Java

  1. Callable interface is part of the java.util.concurrent package where as Runnable interface is part of the java.lang package.
  2. Though both Callable and Runnable are interfaces and both have a single method but that method and its signature is different.

    Callable interface in Java

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

    Runnable interface in Java

    public interface Runnable {
        public abstract void run();
    }
    
  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 between Runnable And Callable that can be noticed from the signatures of the call() and run() method is that you can not declare 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 With Examples
  2. Callable and Future in Java With Examples
  3. Non-Blocking Algorithms
  4. What if run() Method Called Directly Instead of start() Method - Java Multi-Threading
  5. Java Concurrency Interview Questions And Answers

You may also like-

  1. Volatile Keyword in Java With Examples
  2. Interface Default Methods in Java 8
  3. Method reference in Java 8
  4. Lambda Expressions in Java 8
  5. Spliterator in Java
  6. Initializer Block in Java
  7. Access Modifiers in Java - Public, Private, Protected and Default
  8. String Vs StringBuffer Vs StringBuilder in Java