Difference Between Callable and Runnable in Java
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.
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() 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 package import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; // Class// Implementing the Callable interfaceclass CallableExample implements Callable { // Main driver method public static void main(String args[]) { // public Object call() throws Exception Random generator = new Random(); Integer randomNumber = generator.nextInt(5); Thread.sleep(randomNumber * 1000); return randomNumber; } } |
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; // Class// Implementing the Runnable interface public class RunnableImpl implements Runnable { // Main driver method public static void main(String[] args) { // Run method public void run() { // Display message when the thread executes System.out.println( Thread.currentThread().getName() + ", executing run() method!"); // Try block to check if any exception occurs try { throw new FileNotFoundException(); } // Catch block to handle the exception catch (FileNotFoundException e) { // Display message System.out.println("Must catch here!"); // Print the line number // where exception occured e.printStackTrace(); } // Here forcefully it int rem = 1 / 0; } } |
| Runnable interface | Callable interface |
|---|---|
| It is a part of java.lang package since Java 1.0 | It is a part of the java.util.concurrent package since Java 1.5. |
| It cannot return the return of computation. | It can return the result of the parallel processing of a task. |
| It cannot throw a checked Exception. | It can throw a checked Exception. |
| In a runnable interface, one needs to override the run() method in Java. | In order to use Callable, you need to override the call() |
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


