5 Difference between Callable and Runnable in Java

There is a lot of similarity between callable and runnable in java. But there are certain tasks callable can do while runnable can not.When designing a concurrent thread people often ask why to choose one over another.In this post we will discuss the difference between callable and runnable in java.


Difference between Callable and Runnable in Java

1. Checked Exception : Callable's call() method can throw checked exception while  Runnable run() method can  not throw checked exception.

2. Return value : Return type of Runnable run() method is void , so it can not return any value.
while Callable can return the Future object, which represents the life cycle of a task and provides methods to check if the task has been completed or canceled.
   
3. Implementation : Callable needs to implement call() method while Runnable needs to implement run() method.

public interface Runnable {
    void run();
}
public interface Callable<V> {
    V call() throws Exception;
}



4. Availability : Runnable is available in java right from JDK 1.0 while Callable was later added to JDK 5.

5. Execution :  Limitation of Callable interface lies in java is that one can not pass it to Thread as one pass the Runnable instance. There is no constructor defined in the Thread class which accepts a Callable interface. 

Callable vs Runnable
Similarities between Callable and Runnable in Java

1. Executor Framework : Callable and Runnable interface both can be used with Executors framework introduced in java 5.

2. Single Abstract Method : Both Callable and Runnable interface have a single abstract method, which means they can be used in lambda expressions in java 8.



Recap : Difference between Callable and Runnable interface in Java





CallableRunnable
Checked Exceptionthrow Checked ExceptionDoes not throw Checked Exception
Return valueYesNo
Implementation methodcall() run()
AvailabilityJava 5Since jdk 1.0 i.e from beginning


We are finished with the post about the difference between callable and runnable in java. If you know any other difference between Callable and Runnable then please mention in the comments.





About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry