In a Nutshell: JAVA Executor Framework

Posted by Mozammil Amir

Java executor framework is a very useful mechanism introduced in Java 1.5 to manage threads in large-scale applications. Before the executor framework, multithreading applications were developed using thread group and thread pool. Thus, the programmer was solely responsible for thread management and had to take care of thread synchronization, waiting, joining, locking, notification, and handling deadlock.

During project implementation, thread management can be tedious. It, therefore, makes sense to separate thread management and creation from the rest of the application, and Java executor framework allows it. In this framework, programmers can create a thread pool in which ‘worker threads’ execute tasks that can be Runnable or Callable. The executor framework was released with JDK 5 in java.util.concurrent package.

Executor Framework

The Java executor framework is divided into the following three sections:

  • Executor Interfaces: they define the three executor object types.
  • Thread Pools: they are the most common type of executor implementation.
  • Fork/Join: it is a framework (new in JDK 7) to take advantage of multiple processors.

Let us understand all these sections in detail.

Executor Interfaces

There are three interfaces defined for this framework.


A simple interface that supports the launch of new tasks. It provides a single method execute(Runnable Command).


A subinterface of Executor that adds features to manage the lifecycle of both – the individual tasks and executor.
This interface has an additional overloaded submit method, which can take an argument of Runnable as well as Callable type. The submit method returns a Future type of object. For the Runnable type of object, the Future object type is null because the Runnable method’s return type is void, so it cannot return anything other than null. ExecutorService provides a number of methods for managing the shutdown of the executor. To support the immediate shutdown, tasks should handle interrupts correctly.


A subinterface of ExecutorService, supporting future and/or periodic execution of tasks. The ScheduledExecutorService interface supplements the methods of its parent ExecutorService with a schedule, which executes a ‘Runnable’ or ‘Callable’ task after a specified delay. The intervals are defined with scheduleAtFixedRate and scheduleWithFixedDelay methods.

Thread Pool

The Thread Pool backs the Executor Interface. It consists of worker threads and is responsible for executing the tasks. Thread objects use a significant amount of memory. In large applications, allocating and deallocating thread objects results in significant memory management overhead. Worker threads help lower the burden of creating and starting threads. The most common thread pool used is fixed sized thread pool, wherein only a specified number of threads can run at a given point of time. However, if a thread is terminated while it is in use, it is automatically replaced with a new thread. The tasks are fed into the pool through an internal queue, which stores the extra tasks until a thread is available to execute it.

The following thread pools are available in the Java executor framework:

  • FixedThreadPool: it is a factory method used to create a fixed number of threads that can be executed at a given time.
  • CachedThreadPool: the method is used to create an expandable thread pool that would contain new threads as well as old threads that can be reused to execute new tasks.
  • ScheduledThreadPool: the method is used to create threads that are executed periodically, either at a fixed rate or at a fixed delay.
  • SingleThreadExecutor: the method creates an executor to execute a single task at a time.

Below is the example of fixed-size thread pool executor:


ForkJoin is an execution of the ExecutorService interface that helps take advantage of multiple processors. The bigger task is broken into multiple subtasks, and these subtasks are executed in parallel and their results are clubbed together after execution. The fork/join framework is different because it implements a work-stealing algorithm, wherein the worker threads that are in ideal state can steal tasks from other threads that are still in the busy state. The ForkJoinPool class, which is an extension of the AbstractExecutorService class, is the centerpiece of fork/join framework. It helps in not only implementing the work-stealing algorithm but also executing ForkJoinTask processes.

The pseudo code for Fork/Join framework is as follows:

RecursiveAction and RecursiveTask are specialized methods to break a task. Below is an example to read file name in a particular location and break it.

Executor Framework Advantages

Java executor framework abstracts many complexities, which a programmer face while working with raw threads. Some of the advantages that it provides not only to the programmer but also for the applications are:

  • Creating a new thread each time is expensive. The executor framework has many thread pool implementations to cater to the most common concurrency requirements.
  • It provides a standard, comprehensive way to coordinate with the thread(s) submitting the task(s) and the thread(s) performing the task(s). It is easy to note whether the task is completed – wait for it to complete, cancel it, get the result, etc.
  • It provides a simple way of terminating the threads running in a JVM.

Ending Notes

Ever since the introduction of Java executor framework in JDK 5, multithreading has become really easy and convenient. The thread pools provide the advantage of separating the execution of tasks from the creation and management of threads, significantly improving the performance of the application.

That’s all from us folks. Use the executor framework and optimize your applications with simple and convenient multithreading. Do let us know your experience in the comments below.

Until next time!






Related Posts

  • Infinispan Cache: In a NutshellInfinispan Cache: In a Nutshell

    One of the hottest new additions to JBoss galaxy is Infinispan. It is an open source, Java-based data grid platform. Data grids are highly concurrent distributed data structures that allow users to address large…

  • Manager’s Dilema: SAS vs R vs Python

    There are countless articles on this topic already, and I must begin by accepting that I am quite late to this superstar battle. However, every time these champions of analytics…

  • Mobile App Automation Testing using ‘ESPRESSO’

    If you are a Mobile Apps Test Engineer, you cannot overlook the very reliable Google Product i.e. Espresso. Espresso is an automatic UI testing or as we call it “hands…

  • Securing Applications from Vulnerabilities Using NexusSecuring Applications from Vulnerabilities Using Nexus

    Developers need certain libraries/frameworks in their projects most of which can be downloaded from Maven Central. If, however, any file is absent from the central repository, developers have to fetch…

  • Introduction to TypeScriptIntroduction to TypeScript

    What is TypeScript? TypeScript is not a Framework but a programming language that helps in developing a JS framework. It is a superscript for JavaScript, which means you can even…

  • DevOps: In a NutshellDevOps: In a Nutshell

    DevOps is quite a buzzword nowadays. Organizations have formulated a definition for it and have implemented it accordingly. With a perspective of their own, organizations tend to think they have…

Leave a Reply

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