Callable and Runnable Examples – ExecutorService

ExecutorService – Callable and Runnable Example
In this article let us see an example using Runnable and Callable tasks using ExecutorService.

Please refer here for Overview of ExecutorService

Runnable Example
Runnable does not return any value and cannot throw checked exceptions


From the output, we can notice how the executorservice works. We haven’t used Synchronized keyword in any of our methods, but ExecutorService makes sure one thread accesses the methods at a time.

Callable Example
Callable can return Future objects and can throw checked exceptions


We  have used future object to return string, Future object can return Integer, Boolean etc.
Sample example of using Boolean for the above code,


Read More

ExecutorService – An Overview

ExecutorService in Java

What is Executor Service?
Executor service is an interface, which allows us to process tasks asynchronously by threads.

Why ExecutorService over Threads?

ExecutorService has many advantages when compared with using casual threads,

ExecutorService takes care of threads creation for us and also re-uses threads. In java thread creation is expensive as it has to reserve the memory for each threads.  so with ExecutorService, we can create/manage/control life cycle of Threads. Also, ExecutorService provides us with methods like shutdown() and shutdownnow(), When closes the executorservice and will not accept anymore new tasks.

When we use callable in executors, we can get return result for the process.  invokeany() and invokeall() help us to run any or all threads at once.

How to create an ExecutorService?
These are some of the commonly used types of executorservices,

ExecutorService executorService = Executors.newFixedThreadPool(<size>);
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(<size>);
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();

How to create tasks?
We can create tasks that is either Runnable or Callable. Both instances are executed by another thread. So what is the difference between Runnable and Callable tasks?



Runnable doesn’t return a value Callable can return a value
Runnable cannot throw checked exceptions Callable can throw checked exceptions

Once we have implemented the tasks, we use ExecutorService to execute() or submit() the tasks.  What is the difference between execute() and submit()?

execute() – Return type is void
submit() – Returns future object for managing the tasks.

Future Object:

Using Future object we can get the status of callable tasks.  Future.get() method can wait till the callable task is finished and will return the result

invokeAll() – Executes the given tasks, returning a list of Futures holding their status and results when all complete.

invokeAny() – Executes the given tasks, returning the result of one that has completed successfully, without exception.

Shutdown() – Shutdowns the ExecutorService after completing all the submitted tasks and will not accept any new tasks.

ShutdownNow() – Stops all the executing tasks and returns the list of tasks waiting for execution.

We have covered basics of ExecutorService.


Read More