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

[![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-iii/#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 III

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

[Runnable](https://blog.e-zest.com/topic/runnable) [Java](https://blog.e-zest.com/topic/java) [ExecutorService](https://blog.e-zest.com/topic/executorservice) [Callable](https://blog.e-zest.com/topic/callable) [Multithreading](https://blog.e-zest.com/topic/multithreading) [Concurrency API](https://blog.e-zest.com/topic/concurrency-api) [ExecutorService awaitTermination](https://blog.e-zest.com/topic/executorservice-awaittermination) [Future submit callable](https://blog.e-zest.com/topic/future-submit-callable) [Future](https://blog.e-zest.com/topic/future) [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-iii/&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/the-executor-framework-part-iii/&text=The%20Executor%20Framework%20–%20Part%20III>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/the-executor-framework-part-iii/>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/the-executor-framework-part-iii/>

In the Part II of [Executor Framework](https://blog.e-zest.com/blog/the-executor-framework-part-ii/) blog post series, I had written about creating cancellable `Runnable` task and creating `Runnable` task with a timeout with the help of this framework. I had also written about two `submit` methods of [`ExecutorService`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html), which are used to create asynchronous task.

In this blog post, I am writing about the following:

1. `Callable` interface
2. Creating `Callable` task
3. Assigning a timeout to `ExecutorService` after shutdown.

**Callable**

The [`Callable`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html) interface is a part of Concurrency API. Like [`Runnable`](http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html), this interface is used to define a task which will be executed by a thread. The `Callable<T>` interface overcomes the drawbacks of `Runnable`. It has a `call()` method which returns the result of type `T`.

The following are the two drawbacks of `Runnable` interface:

1. The return type of `run()` method, which is used to define what a task should do when a thread runs it, is `void`. The thread executing `Runnable` cannot return a value directly after the execution of `run()` method is over. One way to return a value is to declare an instance variable and set it at the end of `run()` method. However, there is no way to find out whether the execution of run() method is over while trying to retrieve its value. So another instance variable is needed which tells whether the `run()` method execution is complete.
2. The `run()` method cannot throw a checked exception. If the task has to throw a checked exception then it should catch it, wrap it within a `RuntimeException` and re-throw the latter which defies the purpose of checked exceptions.

An implementation of `Callable` interface which counts the sum of `Integer` numbers is given in Listing 1. The call method returns Integer result.

```
public class ComputeSum implements Callable<Integer> {
    private List<Integer> numbers;

    public void setNumbers(List<Integer> numbers) {
        this.numbers = numbers;
    }

    public Integer call() throws Exception {
        int sum = 0;
	for(Integer number : numbers) {
	    System.out.println("Adding number : " + number);
	    sum += number;
	}
	return sum;
    }
}
```

Listing 1. A `Callable<Integer>` implementation

**Creating Callable task**

The `submit(Callable<T> task)` method of [`ExecutorService`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html) allows submission of `Callable` task for asynchronous execution. The [`Future<T>`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html) is returned by this method which is used to obtain the result of execution.

Listing 2 shows a code which generates ten random numbers and uses the `Callable` implementation in Listing 1 to compute their sum. A single worker threaded `ExecutorService` is used to submit the task for execution. The `get()` method of `Future` is called to wait until the sum of numbers is obtained.

```
//Generate 10 random numbers in the range of 0 to 100
Random randomGenerator = new Random();
List<Integer> numbers = new ArrayList<Integer>();
for(int i=1; i<=10; i++) {
    numbers.add(randomGenerator.nextInt(100));
}

ExecutorService service = Executors.newSingleThreadExecutor();

//compute their sum
ComputeSum task = new ComputeSum();
task.setNumbers(numbers);
Future<Integer> future = service.submit(task);

//wait till the sum is computed
try {
    Integer sum = future.get();
    System.out.println("Sum = " + sum);
}
catch (InterruptedException e) {
    e.printStackTrace();
}
catch (ExecutionException e) {
    e.printStackTrace();
}

service.shutdown();
```

Listing 2. Code to create `Callable` task

**Assign a timeout to ExecutorService after shutdown**

In the Part I of this blog post series, I had written about the `shutdown()` and `shutdownNow()` methods used to stop the `ExecutorService`. The `shutdown()` method waits until execution of all active tasks is completed. The maximum time period for which an `ExecutorService` should wait for the active tasks to complete after `shutdown()` is called, can be assigned by calling its `awaitTermination(long timeout, TimeUnit unit)` method. The thread calling this method keeps waiting until the execution of all tasks is complete or timeout period is elapsed or it is interrupted.

The `isShutdown()` method of `ExecutorService` returns true if it is shutdown. The `isTerminated()` method of `ExecutorService` returns true if all tasks are completed after shutdown.

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

### Similar Blog

<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/)

[Interrupting Threads In Java In a Java application, a thread may keep waiting for a long time after executing `wait()` or `Thread.join()` or by `put()` or `take()` ... By Madhura Oak Sep 04, 2012](https://blog.e-zest.com/interrupting-threads-in-java/)

[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.