Difference Between Callable and Runnable in Java - GeeksforGeeks

21 Apr, 2022
java.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread. There are two ways to start a new Thread – Subclass Thread and implement Runnable. There is no need of sub-classing Thread when a task can be done by overriding only run() method of Runnable.
Callable interface and Runnable interface are used to encapsulate tasks supposed to be executed by another thread.
However, Runnable instances can be run by Thread class as well as ExecutorService but Callable instances can only be executed via ExecutorService.
Let us discuss differences between the two above interfaces as defined by discussing them individually later on concluding to major differences in a tabular format.
Callable Interface 
In a callable interface that basically throws a checked exception and returns some results. This is one of the major differences between the upcoming Runnable interface where no value is being returned. In this interface, it simply computes a result else throws an exception if unable to do so.
public interface Callable<V> 
{
  V call() throws exception ;
}
It is declared in the ‘java.util.concurrent‘ package.This interface also contains a single, no-argument method, called call() methodWe can’t create a thread by passing callable as a parameter.Callable can return results. Callable’s call() method contains the “throws Exception” clause, so we can easily propagate checked exceptions further.
It is declared in the ‘java.util.concurrent‘ package.
This interface also contains a single, no-argument method, called call() method
We can’t create a thread by passing callable as a parameter.
Callable can return results. Callable’s call() method contains the “throws Exception” clause, so we can easily propagate checked exceptions further.
Example:
Java
// Java Program to illustrate Callable interface // Importing classes from java.util packageimport java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.Future;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;  // Class// Implementing the Callable interfaceclass CallableMessage implements Callable<String>{  public String call() throws Exception{      return "Hello World!";  } } public class CallableExample{  static ExecutorService executor = Executors.newFixedThreadPool(2);    public static void main(String[] args) throws Exception{        CallableMessage task = new CallableMessage();         Future<String> message = executor.submit(task);         System.out.println(message.get().toString());    }}
Output:
Hello World
Runnable interface 
When an object implementing this interface is used to create a thread, starting the thread causes the object run method to be called in a separately executing thread. The general. contract of this run() method is that it may take any action whatsoever. 
public interface Runnable 
{
  public abstract void run();
}
java.lang.Runnable is an interface and defines only one method called run(). 
It represents a task in Java that is executed by Thread.
There are two ways to start a new thread using Runnable, one is by implementing the Runnable interface and another one is by subclassing the Thread class.
Runnable cannot return the result of computation which is essential if you are performing some computing task in another thread, and Runnable cannot throw checked exceptions.
Example
Java
// Java Program to implement Runnable interface // Importing FileNotFound class from// input output classes bundleimport java.io.FileNotFoundException;import java.util.concurrent.*; // Class// Implementing the Runnable interfaceclass RunnableImpl implements Runnable {   public void run()  {    System.out.println("Hello World from a different thread than Main");  }}public class RunnableExample{    static ExecutorService executor = Executors.newFixedThreadPool(2);  public static void main(String[] args){          // Creating and running runnable task using Thread class          RunnableImpl task = new RunnableImpl();        Thread thread = new Thread(task);          thread.start();          // Creating and running runnable task using Executor Service.          executor.submit(task);    }}
ankitratnam1
java-interfaces
Picked
Technical Scripter 2020
Difference Between
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Prim's and Kruskal's algorithm for MST
Difference between Internal and External fragmentation
Differences and Applications of List, Tuple, Set and Dictionary in Python
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java