0

I have a webservice which in turn calls 10 other webservices and it results in higher response times.So i am using ExecutorService to spawn threads by spring servlet.xml config like below

<bean id="executorService" class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean" scope="prototype">
    <property name="corePoolSize" value="3" />
    <property name="maxPoolSize" value="4" />
    <property name="keepAliveSeconds" value="5" />
</bean>

I am monitoring the threads in JvisualVM and noticing that there are 3 executorservices threads at all times. I am trying to understand if that's going to spin 3 threads for every request that comes thru or will all requests try and use the same 3 corePoolSize thats configured.

If all request coming are going to use 3 threads should increase the corepool size (if so to what number)?

Also, when should i shut down the executor service?

I am new to spinning threads this way,can someone help me understand how it works?

remo
  • 3,326
  • 6
  • 32
  • 50
  • What is the purpose of spinning off threads? Are you trying to execute the 10 webservice calls in parallel? If so, why only create a pool of size 3-4? – Andreas Aug 20 '15 at 02:19
  • Yes, its to run 10 webservice calls in parallel..i intially wanted to see if it works for 3-4 and then bump up. – remo Aug 20 '15 at 02:21

2 Answers2

2

Beans in Spring are singletons, by default, so you only have one instance of executorService which manages a thread pool of size 3-4.

When the pool is created, it will start 3 threads, which are then idle until needed. If more threads are needed, it will use at most 4 threads, so only 1 more than initially created.

This is regardless of how many incoming requests are trying to use the pool.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I need to serve, say a 100 requests..what can be the max value i can set for corePoolSize. Also i specified scope as prototype, will it still create a singleton? – remo Aug 20 '15 at 02:35
  • The max value is controlled by the resources available on your server. Is your webservice bean also a prototype. Probably not, but it doesn't matter. Create the pool small (core=0), with ability to grow as needed (max=100). If you get 3 incoming requests that each start 10 threads, the pool will grow to 30. When the 30 requests complete, the threads will stay idle for keepAliveSeconds, and will then be stopped. – Andreas Aug 20 '15 at 02:42
  • OK got you, If we dont create a bean for executorservice instead to we create an instance for each request will keepAliveSeconds with say 500ms..would that be a viable approach? – remo Aug 20 '15 at 03:52
  • @remo Not really. What's the point of a pool if you never reuse a thread? Just start a thread directly when needed (10 times per request), and the threads end as soon as they are done. If you receive 100 requests, you start 1000 threads and kill the server. ---- **OR** Use a shared pool, that will allow threads to be reused by multiple requests, and will limit the number of concurrent threads. – Andreas Aug 20 '15 at 03:57
1

As @andreas mentioned you will have one instance of executorservice because the bean definition is by default singleton.

The doc says, corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set. So you will always have three threads alive in the pool though they are alive. If there are more requests the pool size will increase to 4 because you have set the maxPoolSize to 4. But the fourth thread ( you do not know which one though) will be out of the pool after being idle for 5 seconds because you have set keepAliveTime to 5.

To answer the later part of your question, you can have look at my answer for a similar question here. It is a performance tuning problem and depends on how your application is handling I/O, CPU, etc.

As you have said that you are calling 10 other web services for one incoming request, so one request is fanning out to 10 requests. Are they network I/O bound or CPU bound? How are you merging them ? How much resource contention is there while merging the responses from 10 downstream services?

Community
  • 1
  • 1
yadab
  • 2,063
  • 1
  • 16
  • 24