Wednesday , September 19 2018
Home / Java / 5 Essential difference between Callable and Runnable interface in Java?

5 Essential 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.

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, 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.

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.

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.

2) Result
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.

5) Execution
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:

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:

Callabe vs Runnable in Java

About admin

Check Also

Internal Working of HashMap in Java

In this article, we will see how hashmap’s get and put method works internally. What ...

Leave a Reply