Top 5 Difference Between Callable and Runnable Interface in Java
Top 5 Difference Between Callable and Runnable Interface in Java
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.
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 Pogrebinsy 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.
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 the 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 a Callable instance in a thread,
just like you execute a Runnable instance, you need an ExecutorService instance.
Let's see them in detail.
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
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:
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.