Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java programming
communitycreator

What is the difference between runnable and callable in Java?

Salman Yousaf

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Both runnable and callable interfaces are designed for classes. Their instances are supposed to be executed by another thread.

However, there are also some differences between these interfaces. Let’s discuss the differences between them by explaining them separately.

Callable interface

A callable interface throws the checked exception and returns the result. A runnable interface, on the other hand, shows the result or throws an exception, but does not do both.

Syntax


public interface Callable<T> 
{
  T call() throws exception ;
}

  • We declare it in the package named java.util.concurrent.
  • It consists of the call() method with no arguments.
  • A programmer cannot create a new thread by passing the callable interface as a parameter.
  • The callable interface can return results.

Example

The example below illustrates the usage of the callable interface.

  • On line #8 we create a class named EdPresso which extends the Callable<String> interface.
  • On line #19 we create a pool of threads of size 5. In the highlighted lines, we create the EdPresso object, which is a list to hold the Future<String> object list.
  • Whenever an asynchronous task is created, a future object is returned.
// importing build-in classes
import java.util.concurrent.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
// class impelementing callable interface
public class EdPresso implements Callable<String> {
@Override // overriding method
public String call() throws Exception {
Thread.sleep(500);
//return the thread name executing this callable task
return Thread.currentThread().getName();
}
// main method starting here
public static void main(String args[]){
// Thread pool size is 5
ExecutorService exe = Executors.newFixedThreadPool(5);
//Create EdPresso instance
Callable<String> callable = new EdPresso();
//create a list to hold the Future object associated with Callable
List<Future<String>> mylist = new ArrayList<Future<String>>();
for(int i=0; i< 50; i++){
//submit Callable tasks to be executed by thread pool
Future<String> store = exe.submit(callable);
//add Future to the list, we can get return value using Future
mylist.add(store);
}
for(Future<String> i: mylist){
try {
// because Future.get() waits for task to get completed
System.out.println(new Date()+ "::"+i.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
//shut down the service now
exe.shutdown();
}
}
Callable Interface Implementation

Runnable interface

It is required to create a thread when a runnable interface is implemented through an object.

The thread contains a single method named run() that does not return any value or accept any parameters.

Syntax


public interface Runnable 
{
  public abstract void run();
}

Example

The example below illustrates the usage of the runnable interface.

As highlighted, we create an object of EdPresso type and an object of thread type thread. It is called after the main method thread completes its execution.

// EdPresso class implementing
// Runnable interface
public class EdPresso implements Runnable {
public static void main(String[] args) {
EdPresso ob1 = new EdPresso();
Thread thread = new Thread(ob1);
thread.start();
System.out.println("Output for code outside the thread");
}
public void run() {
System.out.println("Output for the part running inside the thread ");
}
}
Runnable Interface Implementation
Difference between both interfaces

Runnable interface

Callable interface

The package named java.lang is used in this interface.

It is considered a part of a package named java.util.concurrent.

Can’t throw an exception

This interface can throw an exception

It uses the run() method

Call() method is used in this regard

This interface can’t return the result of any calculation

A runnable interface can return the result of any processed task.

RELATED TAGS

java programming
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring