3

I am adding a new endpoint to a Jersey-based web-service. The logic backing the endpoint needs to make between 10 to 50 calls to another service. The calls are independent and can be parallelized, so I was thinking of using executor service to distribute the work across multiple threads.

I am wondering if I should instantiate an executorService for each request or should there be a shared executorService instance across the webapp. In the later case, how would I decide the number of threads it should run?

0cd
  • 1,400
  • 2
  • 11
  • 22

1 Answers1

2

I am wondering if I should instantiate an executorService for each request or should there be a shared executorService instance across the web app. In the later case, how would I decide the number of threads it should run?

No. In general, you should NOT instantiate the executorService for each web request (option-1) because the server will soon run out of memory and also, thread pool creation on the fly is costly (time-consuming).

So you need to go with the shared instance (option-2) of the executorService by creating it during server startup & configure the thread pool size according to your requirements and by conducting the performance tests.

You can refer here to understand on the thread pools.

developer
  • 19,553
  • 8
  • 40
  • 57
  • What is the best approach to implement the option 2? Share the executorService creating it in (by example) a controller like a static field? Or do you think there is better way to share the ExecutionService? – Rahnzo Jan 11 '18 at 18:45
  • @developer then how come all of the examples online (eg: [the part of this answer that uses `ScheduledExecutorService`](https://stackoverflow.com/a/4691650/3684479)) make the `ScheduledExecutorService` private rather than public? From what you are suggesting here, it should be public so that it can be accessed throughout the app, right? – theyuv Mar 09 '19 at 14:59