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 the difference between Callable and Runnable Interface?

You May Also Like:

What are common multi-threading issues faced by Java Developers?
What are different states of a Thread? What does those states tell us?
What happens when wait() & notify() methods are called?
What is difference between sleep(), yield() and wait() method?
What is difference between intrinsic synchronization and explicit locking using Lock?
What will happen when an exception arises from within a synchronized code block? Will lock be retained or released?
What is difference between Executor submit() and execute() method?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

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