I have a API, which is doing four tasks. If we do sequential this API will take more time.
To increase the performance I have used Executor service framework and submitted four task using executor service.
As i know creating thread is expensive operation, and in executor service thread will be created at the time of submitting the tasks. And threads will be destroyed when we call shutdown method of executor service.
This is the process of each request in this scenario :
- API call
- create executor service
- submit the four future task
- Create the response from all return threads.
- call shutdown executor service method.
- return the response
So my question is creating executor service and threads in each request is correct or not? Or please let me know alternative solution of this.