Spring ThreadPoolTaskExecutor with Callable interface for concurrency programming

Callable is a Java interface that is an useful methodology for control multi-threading returns on concurrency development.

The tutorial will guide you to build a concurrency program with ThreadPoolTaskExecutor of Spring Framework and Callable interface.

I. Technology

– Java 8
– Maven 3.3.9
– Editor: Spring Tool Suite – Version 3.7.3.RELEASE

II. Overview of Spring multi-thread with Callable project

– Program has 5 threads, that works concurrently. At parent thread, for managing the returns of all child threads, We use Callable interface.

1. Structure of project

spring thread pool callable structure

2. Step to do

– Create Spring Boot project.
– Setup ThreadPoolTaskExecutor.
– Create CallableWorker that implements Callable interface.
– Create a Simple Web Controller

III. Practice
1. Create Spring Boot project

Open Spring Tool Suite, choose File->New->Spring Starter Project, input project information as below picture:
spring-callable-project-info

Press Next button, then add needed dependencies:
– For Web MVC dependency, choose Web then select Web as below:

spring-callable-web-mvc-dependency

Press Finish then Spring Boot Project will be created.

Check pom.xml dependency:

2. Setup ThreadPoolTaskExecutor

– Configure application.properties:

– Configure a Spring ThreadPool:

3. Create Callable Worker

4. Create a Simple Web Controller

IV. Run Project

– Build Spring Boot project
Set Goals: clean install

– Run Project:
spring-boot-multi-thread-with-callable-interface-result

– Logs

V. Source Code

springboot-threadpool-callable

By JavaSampleApproach | October 30, 2016.

Last updated on June 4, 2017.


Related Posts


Got Something To Say:

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

*