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.
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.
public interface Callable<T>
{
T call() throws exception ;
}
java.util.concurrent
.call()
method with no arguments.The example below illustrates the usage of the callable interface.
EdPresso
which extends the Callable<String>
interface.EdPresso
object, which is a list to hold the Future<String>
object list.// 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();}}
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.
public interface Runnable
{
public abstract void run();
}
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 ");}}
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