What is difference between Callable and Runnable Interface?

By | August 13, 2019

In this blog post we will discuss about one of the very important java interview question What is difference between Callable and Runnable Interface? 

The java.util.concurrent.Callable interface is similar to java.lang.Runnable to create a custom threads and  both are designed for classes whose instances are potentially  executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception but Callable interface returns a result and throws a checked exception

We can to convert Runnable to Callable Object using the following utility method provided by Executors class

Callable callable = Executors.callable(Runnable task);

You should keep in mind that Callable must be executed using a ExecutorService instead of Thread as shown below.

result = exec.submit(aCallable).get();

Submitting a callable to ExecutorService returns Future Object which represents the lifecycle of a task and provides methods to check if the task has been completed or cancelled, retrieve the results and cancel the task.

Future Interface looks like this –

So eventually, we can cancel a future, get its status or synchronously wait for its completion by invoking blocking get() method.

That’s all about  What is difference between Callable and Runnable Interface?
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *