0% found this document useful (0 votes)
131 views5 pages

Top 5 Difference Between Callable and Runnable Interface in Java

The document discusses the differences between the Runnable and Callable interfaces in Java. The key differences are: 1) Callable can return results from asynchronous task execution, while Runnable cannot return any results. 2) Callable can throw checked exceptions, but Runnable cannot throw checked exceptions. 3) Callable requires an ExecutorService to execute tasks asynchronously, while Runnable can be executed asynchronously by directly submitting it to a Thread.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views5 pages

Top 5 Difference Between Callable and Runnable Interface in Java

The document discusses the differences between the Runnable and Callable interfaces in Java. The key differences are: 1) Callable can return results from asynchronous task execution, while Runnable cannot return any results. 2) Callable can throw checked exceptions, but Runnable cannot throw checked exceptions. 3) Callable requires an ExecutorService to execute tasks asynchronously, while Runnable can be executed asynchronously by directly submitting it to a Thread.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Top 5 Difference Between Callable and Runnable Interface in Java

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

Callable callable = Executors.callable(Runnable task);

4) 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.

You might also like