Difference between Runnable and Callable interface in Java
Runnable was the only way to implement a task in Java which can be executed in parallel before JDK 1.5 adds Callable. Just like Runnable, Callable also defines a single call() method but unlike run() it can return values and throw exceptions.
Callable interface is newer than Runnable interface and added on Java 5 release. Though both Callable and Runnable interface are designed to represent task, which can be executed by any thread, there is some significant difference between them. Major difference between Callable and Runnable interface is that Callable can return result of operation performed inside call() method, which was one of the limitation with Runnable interface.
Another significant difference between Runnable and Callable interface is ability to throw checked exception. Callable interface can throw checked exception because its call method throws Exception. Commonly FutureTask is used along with Callable to get result of asynchronous computation task performed in call() method.
Callable vs Runnable interface
1) Runnable interface is older than Callable, it is from JDK 1.0, while Callable is added on Java 5.0.
2) Runnable interface has run() method to define task while Callable interface uses call() method for task definition.
3) run() method does not return any value, it's return type is void while call method returns value. Callable interface is a generic parameterized interface and Type of value is provided, when instance of Callable implementation is created.
4) Another difference on run and call method is that run method can not throw checked exception, while call method can throw checked exception in Java.
Callable interface is newer than Runnable interface and added on Java 5 release. Though both Callable and Runnable interface are designed to represent task, which can be executed by any thread, there is some significant difference between them. Major difference between Callable and Runnable interface is that Callable can return result of operation performed inside call() method, which was one of the limitation with Runnable interface.
Another significant difference between Runnable and Callable interface is ability to throw checked exception. Callable interface can throw checked exception because its call method throws Exception. Commonly FutureTask is used along with Callable to get result of asynchronous computation task performed in call() method.
Callable vs Runnable interface
1) Runnable interface is older than Callable, it is from JDK 1.0, while Callable is added on Java 5.0.
2) Runnable interface has run() method to define task while Callable interface uses call() method for task definition.
3) run() method does not return any value, it's return type is void while call method returns value. Callable interface is a generic parameterized interface and Type of value is provided, when instance of Callable implementation is created.
4) Another difference on run and call method is that run method can not throw checked exception, while call method can throw checked exception in Java.
Comments
Post a Comment