What is the difference between runnable and callable in Java?
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, does not return a result and cannot throw a checked exception.
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
EdPressowhich extends theCallable<String>interface. - On line #19 we create a pool of threads of size 5. In the highlighted lines, we create the
EdPressoobject, which is a list to hold theFuture<String>object list. - Whenever an asynchronous task is created, a future object is returned.
// importing build-in classesimport java.util.concurrent.*;import java.util.ArrayList;import java.util.Date;import java.util.List;// class impelementing callable interfacepublic class EdPresso implements Callable<String> {@Override // overriding methodpublic String call() throws Exception {Thread.sleep(500);//return the thread name executing this callable taskreturn Thread.currentThread().getName();}// main method starting herepublic static void main(String args[]){// Thread pool size is 5ExecutorService exe = Executors.newFixedThreadPool(5);//Create EdPresso instanceCallable<String> callable = new EdPresso();//create a list to hold the Future object associated with CallableList<Future<String>> mylist = new ArrayList<Future<String>>();for(int i=0; i< 50; i++){//submit Callable tasks to be executed by thread poolFuture<String> store = exe.submit(callable);//add Future to the list, we can get return value using Futuremylist.add(store);}for(Future<String> i: mylist){try {// because Future.get() waits for task to get completedSystem.out.println(new Date()+ "::"+i.get());} catch (InterruptedException | ExecutionException e) {e.printStackTrace();}}//shut down the service nowexe.shutdown();}}
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 interfacepublic 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 ");}}
Difference between both interfaces
Runnable interface | Callable interface |
The package named | It is considered a part of a package named |
Can’t throw an exception | This interface can throw an exception |
It uses the |
|
This interface can’t return the result of any calculation | A runnable interface can return the result of any processed task. |
Free Resources