Introduction to Executor Framework

1. Introduction

I have been thinking of writing on this since long, based on my experience with it in the past.

Executor Framework was released with JDK 5 in package java.util.concurrent, to ease the pain and responsibility of the developers to individually take care of the following thread activities –

  • Thread synchronization
  • Thread waiting
  • Thread joining
  • Thread locking
  • Thread notification
  • Handling dead lock

Executor Framework provides an abstraction layer over all the internal thread management tasks, such that the developer needs to focus only on the implementation of the business logic. The Executor Framework very elegantly manages the entire concurrent execution flow of the threads.


2. Thread pool

Creating a thread in java is a very expensive process which includes memory overhead also. So, it’s a good idea if we can re-use these threads once created. Executor framework implementation in java uses thread pools consisting of such re-usable worker threads. The entire life cycle of these worker threads is managed by the Executor framework, leading to an efficient memory management compared to the early approaches.

3. Implementation

Below is the simplified step-wise process to help you implement the same –

3.1 Creating the executor

a. To create a fixed pool of 10 threads –

b. To create a cached pool

Performance-wise, I found the fixed pool approach better, but quite good analysis might be needed to initialize the thread count in the fixed pool.

3.2 Create the tasks

Once the executor is created, its time to create the tasks, which can be instances of either Runnable or Callable. These tasks are internally queue and then processed for concurrent execution.

3.3 Submit the tasks to the executor

Once the tasks are created, they need to be submitted to the executor by using either submit or execute method. The tasks are then picked up from the executor queue and run concurrently.

With submit, instance of FutureTask is returned, which provides added information about task processing. Future class is nothing but the implementation of the FutureTask.

Following are the ways to delegate tasks for execution to an ExecutorService –

  • execute(Runnable)
  • submit(Runnable)
  • submit(Callable)
  • invokeAny(…)
  • invokeAll(…)

Executor is responsible for managing the task’s execution. For a FixedThreadPool, no extra thread more than the configured number is created during execution. If there is no free thread available the task has to wait and then execute when one thread is free. However, for a CachedThreadPool, existing threads are reused if available. If there no free thread is available, a new one is created (which is quite expensive) and added to the pool to complete the new task. Threads that have been idle for longer than a timeout period gets removed automatically from the pool.

3.4 Shutdown the executor

The executor can be explicitly terminated with its shutdown method.

4. Difference between Runnable and Callable

Below is how we can create a Callable instead of a Runnable.

With Callable, the resulting Future object (taskResult) will hold some value, which is not the case with Runnable. With Runnable, the resulting Future object will hold null if the task completes successfully.

Note that Future.get() will wait for the Callable task to get completed.

We can also create a collection of Callables and invoke all of them concurrently with the invokeAll method provided by the Executor Service instead of submit or execute. For example –

Runnable has been around since Java 1.0, but Callable was only introduced in Java 1.5 to handle use-cases that Runnable did not support.

5. Submit or invokeAll?

  • submit does not accept a collection of Callable, whereas invokeAll or invokeAny can.
  • With submit, we are NOT waiting for the tasks to complete. Whereas with invokeAll, we are waiting for the tasks to complete.

From the Java Doc –

invokeAll executes the given tasks, returning a list of Futures, holding their status and results when all complete.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.