---
title: The Executor Framework – Part V
description: The Executor Framework – Part V
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/the-executor-framework-part-v/#top)

Hit enter to search or ESC to close

Insights

e-Zest members share technology ideas to foster digital transformation.

- [Blog](https://blog.e-zest.com)
- [News](https://blog.e-zest.com/news)
- [Podcasts](https://www.e-zest.com/podcasts)
- [Healthcare](https://blog.e-zest.com/healthcare)
- [CSR](https://blog.e-zest.com/csr)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [Media Kit](https://www.e-zest.com/media-kit)

# The Executor Framework – Part V

[Madhura Oak](https://blog.e-zest.com/author/madhura-oak)  Apr 02, 2013

[Executor](https://blog.e-zest.com/topic/executor) [Executors.newCachedThreadPool](https://blog.e-zest.com/topic/executors-newcachedthreadpool) [Java](https://blog.e-zest.com/topic/java) [Callable](https://blog.e-zest.com/topic/callable) [ExecutorService invokeAny](https://blog.e-zest.com/topic/executorservice-invokeany) [Multithreading](https://blog.e-zest.com/topic/multithreading) [BlockingQueue](https://blog.e-zest.com/topic/blockingqueue) [Concurrency API](https://blog.e-zest.com/topic/concurrency-api) [ThreadPoolExecutor](https://blog.e-zest.com/topic/threadpoolexecutor) [ExecutorService callable](https://blog.e-zest.com/topic/executorservice-callable) [Executors](https://blog.e-zest.com/topic/executors) [Technology](https://blog.e-zest.com/topic/technology)

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/the-executor-framework-part-v/&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/the-executor-framework-part-v/&text=The%20Executor%20Framework%20–%20Part%20V>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/the-executor-framework-part-v/>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/the-executor-framework-part-v/>

So far in the Executor Framework blog post series, I have covered the following topics:

1. [Creating a single worker thread for sequential execution of tasks](https://blog.e-zest.com/blog/the-executor-framework-part-i/)
2. [Creating a thread pool of fixed number of threads](https://blog.e-zest.com/blog/the-executor-framework-part-i/)
3. [Creating asynchronous task](https://blog.e-zest.com/blog/the-executor-framework-part-ii/)
4. [Creating cancellable `Runnable` task](https://blog.e-zest.com/blog/the-executor-framework-part-ii/)
5. [Creating `Runnable` task with a timeout](https://blog.e-zest.com/blog/the-executor-framework-part-ii/)
6. [`Callable` interface](https://blog.e-zest.com/blog/the-executor-framework-part-iii/)
7. [creating `Callable` task](https://blog.e-zest.com/blog/the-executor-framework-part-iii/)
8. [assigning a timeout to `ExecutorService` after shutdown](https://blog.e-zest.com/blog/the-executor-framework-part-iii/)
9. [use of `ExecutorService` to submit multiple Callable tasks for bulk execution](https://blog.e-zest.com/blog/the-executor-framework-part-iv/)
10. [assigning a timeout to `Callable` tasks submitted for bulk execution](https://blog.e-zest.com/blog/the-executor-framework-part-iv/)

In this blog post, I’m writing about:

1. Executing a single task from a collection of tasks
2. Creating a `Callable` task from Runnable task
3. Cached Thread Pool
4. `ThreadPoolExecutor`

**Executing a single task from a collection of tasks**

The `invokeAny(Collection<? extends Callable<T>> tasks)` method of `ExecutorService` submits a collection of `Callable` tasks for execution and stops after any one task from the collection has executed normally or with exception. The result of the completed task is returned by this method. The remaining tasks which not started are cancelled. If any other task is in execution then the thread running it will throw `InterruptedException`. Appropriate logic should be implemented in the code to stop the task if `InterruptedException` is thrown. If the thread running other task cannot be interrupted then the task cannot be cancelled.

The collection of tasks should not be modified while this method is under execution. To ensure this `Collections.unmodifiableCollection(tasks)` can be called to get the unmodifiable collection.

The tasks can be interrupted while waiting. If any of the task is interrupted this method throws `InterruptedException`. If no task is completed successfully this method throws `ExecutionException`.

The `invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)` method of `ExecutorService` is used to assign a timeout period for execution. If any of the task is not executed before the timeout period is elapsed then this method throws `TimeoutException`.

**Creating a Callable task from Runnable task**

The `callable(Runnable task)` method of `Executors` allows a `Callable` task to be created from a `Runnable` task. The `Callable` task returns `null` after execution. A return result instead of null can be specified by calling the `callable(Runnable task, T result)` method of `Executors`.

**Cached Thread Pool**

The fixed thread pool creates the fixed number of threads and keeps them alive until the `ExecutorService` is shutdown. The cached thread pool creates new threads and adds them to the pool as they are needed instead of a fixed number of threads. If the previously created threads exist and are available then they are reused. The cached thread pool should be used when multiple concurrent short-lived asynchronous tasks have to be executed. If a thread remains unused for a minute then it is terminated and removed from the pool. Thus no resources are consumed if the cached thread pool is idle for more than a minute. The `Executors.newCachedThreadPool()` method creates a cached thread pool and returns a `ExecutorService`.

**ThreadPoolExecutor**

The `ThreadPoolExecutor` class is an implementation of `ExecutorService`. The `Executors` factory methods provide preconfigured settings and should be preferred instead of creating this class instance directly. If you want fine tuning of thread pools which is not provided by the `Executors` factory methods then only you should use this class.

There are many constructors of `ThreadPoolExecutor` class which allow fine tuning. The core and maximum pool size are specified as first and second argument respectively in these constructors. When a task is submitted for execution to `ThreadPoolExecutor` and if the number of threads is less than the core pool size then a new thread is started and assigned the task even if an existing thread is idle and available.

By default, the core threads are created and started only when a new task is submitted. However, a core thread can be created and started before the task is submitted to the `ThreadPoolExecutor` by calling its `preStartCoreThread()` method. This method creates a core thread if the number of core threads is less than the core pool size. If a core thread is started, this method returns true. All core threads can be pre-created and pre-started by calling the `preStartAllCoreThreads()` method of `ThreadPoolExecutor`. These threads will be idle till a task is assigned to them.

The maximum pool size specifies the maximum number of threads that can exist in a pool at any time. If the threads running in the pool are equal or greater than the core pool size and the queue which holds the submitted tasks is bounded and full and the number of threads in the pool has not reached the maximum pool size then a new thread is created and added in the pool. The maximum value that can be assigned to the maximum pool size is `Integer.MAX_VALUE`.

A fixed sized thread pool is created by specifying same number to the core and maximum pool size.

The `setCorePoolSize(int size)` and `setMaximumPoolSize(int size)` allow the core and maximum pool size of `ThreadPoolExecutor` to be changed dynamically.

The third and fourth argument passed in all `ThreadPoolExecutor` constructors is the `keepAliveTime` and its `TimeUnit` respectively. When the number of threads in the pool exceeds the core pool size, the excess threads which are idle are kept alive for the `keepAliveTime`. If they remain unutilized after their `keepAliveTime` has elapsed then they are terminated. This `keepAliveTime` timeout can also be assigned to the core threads by calling the `allowCoreThreadTimeOut(true)` method of `ThreadPoolExecutor`. The `keepAliveTime` should be a non-zero value if this method is called. If false is passed as argument to this method, then the core threads are never terminated until the `ThreadPoolExecutor` shuts down. The `keepAliveTime` can be changed later by calling the `setKeepAliveTime(long time, TimeUnit unit)` method of `ExecutorService`.

A `BlockingQueue<Runnable>` which holds the submitted tasks is the fifth argument passed to all `ThreadPoolExecutor` constructors. If this queue size is less than the core pool size then a new thread is assigned the task and added to the pool. If the number of threads running is more than core pool size, then the submitted tasks are held by the queue until they are assigned to any available thread for execution. Three types of queues that can be used – direct handoffs, unbounded and bounded queues.

`SynchronousQueue` is the default queue used when the factory methods of `Executors` are called to create an `ExecutorService`. This queue directly hands off a task to an available thread whenever a task is submitted for execution. If no thread is available then a new thread is constructed provided the number of threads in the pool is less than maximum pool size. If the number of threads in the pool exceeds maximum pool size then the submitted task is rejected according to the rejection policy.

When unbounded queue such as `LinkedBlockingQueue` is used to hold the submitted tasks, the number of threads will never grow beyond the core pool size. The new tasks will wait in the queue as long as a core thread becomes available for executing it. When the tasks are submitted faster than they are processed, the size of the queue will keep growing.

When a bounded queue such as `ArrayBlockingQueue` is used to hold the submitted tasks a new thread is created and added to the pool when the following conditions are true:

1. the queue is full
2. no core thread is available for execution
3. the number of threads is more than the core pool size
4. the number of threads is less than the maximum pool size

Tuning of `ThreadPoolExecutor`, which uses bounded queue, is difficult and should be done only by an expert programmer.

If the number of threads reach the maximum pool size while using bounded queue then the submitted task is rejected. The `ThreadPoolExecutor.AbortPolicy` is the rejection policy used by default and it throws `RejectedExecutionException`.

Other policies for handling rejected tasks can be specified by using the appropriate constructor of `ThreadPoolExecutor`.

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/the-executor-framework-part-v/&text=The%20Executor%20Framework%20–%20Part%20V>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/the-executor-framework-part-v/>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/the-executor-framework-part-v/&title=&summary=&source=>

### Similar Blog

<https://blog.e-zest.com/the-executor-framework-part-vii/>

[The Executor Framework – Part VII](https://blog.e-zest.com/the-executor-framework-part-vii/)

[In the last blog post on](https://blog.e-zest.com/the-executor-framework-part-vii/)[Executor Framework](https://blog.e-zest.com/blog/the-executor-framework-part-vi/), I had written about [ThreadPoolExecutor](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html). In this blog post, I’m writing about [...](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html)

By Madhura Oak Oct 07, 2013

<https://blog.e-zest.com/the-executor-framework-part-ii/>

[The Executor Framework – Part II](https://blog.e-zest.com/the-executor-framework-part-ii/)

[In the Part I of](https://blog.e-zest.com/the-executor-framework-part-ii/)[Executor Framework](https://blog.e-zest.com/blog/the-executor-framework-part-i/) blog post series, I had written how it can be used to create a single worker thread for ...

By Madhura Oak Feb 18, 2013

[The Executor Framework – Part I Multithreading is one of the important features of Java programming language. A thread runs a task and thus multiple threads ... By Madhura Oak Feb 11, 2013](https://blog.e-zest.com/the-executor-framework-part-i/)

[Oracle NoSQL Database (ONDB) Relational databases have been around us for more than 20 years now. Needless to say, they are mature and an obvious choice for ... By Madhura Oak Dec 24, 2013](https://blog.e-zest.com/oracle-nosql-database-ondb/)

[Session State Isolation Techniques Isolation of data in a session is the trickiest part of session management. A session may constitute of multiple requests. ... By Madhura Oak Dec 02, 2013](https://blog.e-zest.com/session-state-isolation-techniques/)

[Session Management There are three ways of session management in n-tier applications: By Madhura Oak Nov 27, 2013](https://blog.e-zest.com/session-management/)

[Transaction and Query timeout Performance is one of the non-functional requirements of an application. It defines the acceptable maximum response time for ... By Madhura Oak Nov 25, 2013](https://blog.e-zest.com/transaction-and-query-timeout/)

[![Multiple Inheritance in Domain Model Design](https://blog.e-zest.com/hs-fs/file-3586010320-png/blog-files/uml-class-diagram-%E2%80%93-bridge-pattern.png) Multiple Inheritance in Domain Model Design While using multiple inheritance in domain model design you need to be cautious as programming languages such as Java do not ... By Madhura Oak Nov 13, 2013](https://blog.e-zest.com/multiple-inheritance-in-domain-model-design/)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.