It may be a silly question but i am newbie on this, I am creating a web application where notifications are send by Email. For this i am planning to use Executor Service. I am bit confused in Executor Service , should i use (ExecutorService es =Executors.newFixedThreadPool(10)) approach directly or i have to create it in a single place and get same ExecutorService object everytime ?
-
I recommend you to use same ExecuteService because you can handle the count of thread easily. – bitli Apr 25 '14 at 21:11
-
@bitli so ExecutorService es =Executors.newFixedThreadPool(10) should be call only one time ? – vaib Apr 25 '14 at 21:19
1 Answers
You definitely need to reuse the same ExecutorService object. And it probably does not need 10 threads, 1 should be enough.
Rather than defining the ExecutorService as a static field (though you can do that), you can define it as a Spring service if you are using Spring, or as a field in a singleton: that should have a bit more predictable behavior in a webapp.
That being said, ideally you should not put this functionality in your webapp at all. A webapp will be deployed on a web server, which is already managing a thread pool. Ideally there should be just one thread pool management per application. Certain things regarding your new thread might work unexpectedly in some cases, such as its termination.
Ideally, you could have your webapp send somehow information to another, separately running app and dedicated to that purpose, about the emails that need to be sent. A queue would probably be the ideal choice for communication technology between the two.

- 775
- 5
- 11
-
1yotsov i dont not want to engage server's thread for long time since each thread represent a request if i hold it for some time taking process the response will get delayed. That is why i am creating another thread pool management which will take the information from request thread and will do further processing. – vaib Apr 25 '14 at 21:46
-
Your motivation was clear to me, and is natural. I was not saying you should do email sending synchronously. – yotsov Apr 25 '14 at 21:53