Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaDifference Between Callable and Runnable in Java

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.

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 ;
}
  1. It is declared in the ‘java.util.concurrent package.
  2. This interface also contains a single, no-argument method, called call() method
  3. We can’t create a thread by passing callable as a parameter.
  4. 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.Future;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
  
// Class
// Implementing the Callable interface
class 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 bundle
import java.io.FileNotFoundException;
import java.util.concurrent.*;
 
// Class
// Implementing the Runnable interface
class 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);
    }
}


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 result 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()
RELATED ARTICLES

Most Popular

Recent Comments