Reputation: 3484
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?
Upvotes: 0
Views: 706
Reputation: 2143
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?
Upvotes: 1
Reputation: 159086
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.
Upvotes: 2