Differences between Callable and Runnable in Java is a frequently asked Java concurrency interview question and that is the topic of this post.
Runnable and Callable interfaces in Java
Runnable interface is around from JDK 1.0 where as Callable was added much later in Java 5 along with many other concurrent features like ConcurrentHashMap, BlockingQueue, ExecutorService.
If you see basic functionality, both Callable and Runnable interfaces in Java are implemented by any class whose instances are to be executed by another thread. Since Callable is added later so it is obvious that it would have some extra features which were not there in Runnable. Some of the added features in Callable are-
- It can return value.
- It can throw checked exception.
Which are not there in Runnable interface in Java. These features make Callable an excellent choice if you have to run a task that involves extensive computation of a value that can be returned later.
Runnable Vs Callable in Java
- Callable interface is part of the java.util.concurrent package where as Runnable interface is part of the java.lang package.
- Though both Callable and Runnable are interfaces and both have a single method but that method and its
signature is different.
Callable interface in Java
public interface Callable<V> { V call() throws Exception; }
Runnable interface in Java
public interface Runnable { public abstract void run(); }
- If you have noticed the signature of call method in the Callable interface you can see that call method
can return value.
V call() throws Exception
Here V is the computed result. Callable is a generic interface and type is provided at the time of creating an instance of Callable implementation.As example
Callable<Integer> callableObj = new Callable<Integer>() { @Override public Integer call() throws Exception { return 5; } };
run() method of the Runnable interface doesn't return any value, return type for the run method is void.
- Another difference between Runnable And Callable that can be noticed from the signatures of the call() and run() method is that you can not declare a checked exception with a throws clause in run method.
This statement will give compile time error-
public void run() throws InterruptedException
With call() method checked exception can be given with throws clause.This statement is valid-
public Integer call() throws InterruptedException
-
In order to run a runnable task options are-
- Thread class has a constructor that takes Runnable as parameter.
- Executor interface has execute method which takes Runnable as parameter.
- ExecutorService has submit method which takes Runnable as parameter.
- Thread class doesn't have any constructor that takes Callable as parameter.
- ExecutorService has submit method which takes Callable as parameter.
- ExecutorService also has invokeAll and invokeAny methods that take Callable as parameter.
- Executors class has callable method that can convert Runnable to Callable.
Callable callable = Executors.callable(Runnable task);
That's all for this topic Difference Between Runnable And Callable in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-