The difference between Callable and Runnable is one of the most frequently asked multi-threading and concurrency interview question in Java world. I remember, it was 2007 when I first heard about Callable interface and that too on a telephonic interview. Till then, I was happy using Runnable to implement threads and just started paying attention to Java 1.5, as most of the application by then using Java 1.4. That one interview question encouraged me to learn more about several other useful features introduced in Java 5 concurrency library e.g. CountDownLatch , CyclicBarrier , Semaphore , Atomic variables, and Thread pool. This is one of the reasons I always encourage Java developer to give/take regular interviews, just to update your knowledge.
In this article, I'll discuss how to answer this question by highlighting some key differences between Runnable and Callable in Java. I didn't know all these points at that time but my research after that helped to learn more about the Callable interface.
1. Callable can return result
2. Callable can throw checked Exception.
In fact, Callable interface was introduced in Java 1.5 to address the above two limitations of Runnable interface i.e. Runnable cannot return the result of computation which is essential if you are performing some computing task in another thread and Runnable cannot throw checked exception. Now, Java's multi-threading API has got best of both worlds. You can further read Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about when to use Runnable vs Callable while coding multi-threaded Java applications.
As I said, what is the difference between Callable and Runnable is also one of the frequently asked multithreading interview questions in Java, so knowing the difference not only help you to write better code but also to do well on interviews? Let's see a couple of more differences between these two interfaces to understand them better.
Runnable vs Callable
Before looking at the difference between the Runnable and Callable interface, let's look at the similarities between them, they are indeed quite similar.
1) The most common thing between them is that both are used to encapsulate code which needs to be run in parallel on a separate thread.
2) The second similarity between them is that bot can be used with Executors framework introduced in Java 5. Executors framework defines ExecutorService interface which can accept and execute Runnable and Callable.
3) You can also convert Runnable to Callable by using the following utility method provided by Executors class
As I said, both are used to encapsulate the code, you want to run in a separate thread, but there is a limitation, you cannot execute Callable instance in a thread, just like you execute a Runnable instance, you need an ExecutorService instance. Let's see them in detail.
Callable callable = Executors.callable(Runnable task);
4) The fourth similarity between Runnable and Callable interface is that both are SAM type i.e. they have a single abstract method, which means they can be used in lambda expression in Java 8 as shown below.
new Thread( () -> System.out.println("Runnable") ).start()
Now, we know the similarities between Runnable and Callable interface, it's time to see the differences again. On the same note, you can also use the Java Programming Interview exposed book to check out similar questions for more practice.
Difference between Runnable vs Callable interface
Here are some important difference between these two key interfaces of Java's multithreading API in the point format.
1) Existence and Availability
First and most important difference between Runnable and Callable interface is that Runnable is available in Java right from the beginning i.e. JDK 1.0 while Callable was later added to Java 5, so you cannot use Callable before Java 5.
The second key difference between Callable and Runnable is that Callable can return the result of parallel processing of a task. It returns a Future object , which represents the lifecycle of a task and provides methods to check if the task has been completed or canceled, retrieve the result or cancel the task. Since return type of Runnable' s run() method is void, it cannot return anything.
3) Checked Exception
Another worth noting difference between these two interfaces is that Callable's call() method can throw Checked exception while Runnable's run() method cannot throw checked exception.
4) The call() vs run() methods
In order to use Callable, you need to override the call() method while in order to use Runnable interface you need to override the run() method in Java.
There is one limitation while using Callable interface in Java that you cannot pass it to Thread as you pass the Runnable instance. There is no constructor defined in the Thread class which accepts a Callable interface. So in order to execute a Callable instance you need to use the ExecutorService interface of Java 5 Executor framework. This interface defines submit() method which accepts a Callable instance and return a Future object which holds the result of computation as shown below:
Future result = exec.submit(aCallable); result = holder.get();
Remember, Future.get() is a blocking method and blocks until execution is finished, so you should always call this method with a timeout to avoid deadlock or livelock in your application.
In short, here is the key difference between Runnable and Callable in Java:
That's all about the difference between a Callable and Runnable interface in Java . Apart from the basic fact that Callable was added later in Java 5, you should remember that Callable can return result and throw checked exception but you need ExecutorService to execute Callable instance. Good thing is that both Runnable and Callable are SAM type so you can use them in lambda expressions.
Similar frequently asked thread interview questions you may like
- Difference between start() and run() method in Java? ( answer )
- Difference between execute() and submit() method of Executor in Java? ( answer )
- How to join multiple threads in Java? ( answer )
- How to stop a thread in Java? ( answer )
- How to solve producer consumer problem using blocking queue in Java? ( solution )
- Top 50 Thread Interview Questions for experienced programmers ( list )
ReferencesJava Concurrency in Practice by Brian Goetz