Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manimuttu/48089e91af219f716014 to your computer and use it in GitHub Desktop.
Save manimuttu/48089e91af219f716014 to your computer and use it in GitHub Desktop.
Difference between Callable and Runnable in Java - Interview question
http://java67.blogspot.sg/2013/01/difference-between-callable-and-runnable-java.html
Difference between CallableRunnable interface in Java is one of the interesting question from my list of Top 15 Java multi-threading questions, and it’s also very popular in various Java Interviews. Callable interface is newer than Runnable interface and added on Java 5 release along with other major changes e.g. Generics, Enum, Static imports and variable argument method. Though both CallableRunnable interface are designed to represent task, which can be executed by any thread, there is some significant difference between them. In my opinion, 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 RunnableCallable interface is ability to throw checked exception. Callable interface can throw checked exception because it's call method throws Exception. By the way sometime this question is also asked as follow-up question of another classic difference between Runnable and Thread in Java. Commonly FutureTask is used along with Callable to get result of asynchronous computation task performed in call() method.
Callable vs Runnable interface
As I explained major differences between CallableRunnable interface in last section. Sometime this question is also asked as difference between call()run()method in Java. All the points discussed here is equally related to that question as well.Let's see them in point format for better understanding :
Runnable interface is older than Callable, there from JDK 1.0, while Callable is added on Java 5.0.
Runnable interface has run() method to define task while Callable interface uses call() method for task definition.
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.
That's all on Difference between Callable and Runnable interface in Java or difference between call()run() method. Both are very useful interface from core Java and good understanding of where to use RunnableCallable is must for any good Java developer. In next article we will see example of Callable interface along with FutureTask to learn How to use Callable interface in Java.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment