Top 5 Difference Between Callable and Runnable Interface in Java (original) (raw)
The difference between Callable and Runnable is one of the most frequently asked multi-threading and concurrency interview questions in the Java world. I remember it was 2007 when I first heard about the 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 applications 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 like CountDownLatch, CyclicBarrier, Semaphore, Atomic variables, and Thread pool. This is one of the reasons I always encourage Java developers 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.
Even though both Callable and Runnable interface are used to encapsulate task supposed to be executed by another thread, there is two key difference between Callable and Runnable interface:
1. Callable can return result
2. Callable can throw checked Exception.
In fact, a Callable interface was introduced in Java 1.5 to address the above two limitations of the 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 the best of both worlds.
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 helps 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.
Btw, if you are serious about mastering Java multi-threading and concurrency then I also suggest you take a look at the Java Multithreading, Concurrency, and Performance Optimization course by Michael Pogrebinsky on Udemy. It's an advanced Java course to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance.
Similarities between Runnable and Callable in Java
Before looking at the difference between the Runnable and Callable interface, let's look at the similarities between them, they are indeed quite similar.
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.
The second similarity between them is that bot can be used with the Executors framework introduced in Java 5. Executors framework defines ExecutorService interface which can accept and execute Runnable and Callable.
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 a 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);
- The fourth similarity between the 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 see Multithreading and Parallel Computing in Java course on Udemy to learn more about essential thread concepts like this.
Difference between Runnable vs Callable interface
Here are some important differences between these two key interfaces of Java's multithreading API in the point format.
1) Existence and Availability
The 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.
2) Result
The second key difference between Callable and Runnable is that Callable can return the result of the 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 the 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 the Runnable interface you need to override the run() method in Java.
5) Execution
There is one limitation while using the 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 the Java 5 Executor framework. This interface defines the submit() method which accepts a Callable instance and returns 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 results and throw checked exceptions but you need ExecutorService to execute the Callable instance. The good thing is that both Runnable and Callable are SAM type so you can use them in lambda expressions.
Further Learning
Multithreading and Parallel Computing in Java
Java Concurrency in Practice - The Book
Applying Concurrency and Multi-threading to Common Java Patterns
Java Concurrency in Practice Course by Heinz Kabutz
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 the producer-consumer problem using a blocking queue in Java? (solution)
- Top 50 Thread Interview Questions for experienced programmers (list)
- 10 Free Java Courses for Beginners and Intermediate developers (courses)
- How to avoid deadlock in Java? (answer)
- 10 Java Multithreading and Concurrency Best Practices (article)
- Understanding the flow of data and code in Java program (answer)
- Top 5 Books to Master Concurrency in Java (books)
- Is Java Concurrency in Practice still valid (answer)
- Difference between CyclicBarrier and CountDownLatch in Java? (answer)
- How Happens Before works in Java? (answer)
- 10 Tips to become a better Java Developer (tips)
- How to do inter-thread communication in Java using wait-notify? (answer)
- Top 5 Courses to Learn Java Multithreading in-depth (courses)
Thanks for reading this article of far. If you find this Callable vs Runnable comparison then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are a Java beginner and want to learn multithreading and concurrency and looking for some free courses to start with then you can also check out this free Java Multithreading course on Udemy. It is a good free course to learn Java Concurrency as well.
And lastly, Let me know in comments if you have used Runnable or Callable in Java? Which one is your favorite?