BalaB BalaB - 1 year ago 425
Java Question

Spring ThreadPoolTaskExecutor vs Java Executorservice cachedthreadpool

What are the pros and cons of using

Spring ThreadPoolTaskExecutor vs Java Executorservice

even though spring is wrapper of Java concurrency.

Just would like to know the flexibility in using them.

Answer Source

One of the added Advantage of using ThreadPoolTaskExecutor of spring is that it is well suited for management and monitoring (e.g. through JMX), providing several useful attributes: "corePoolSize", "maxPoolSize", "keepAliveSeconds" (all supporting updates at runtime); "poolSize", "activeCount".

apart from that it is obviously simple to use if you already have spring injections implemented in your application. by using it you can directly inject thread pool by setter injection like below:

<bean id="taskExecutor"
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="WaitForTasksToCompleteOnShutdown" value="true" />

ThreadPoolTaskExecutor Doc

on the other hand ExecutorService CachedThreadPool is good utility to share your most recent under utilized threads ( Under 60 seconds ). It is important to point out that CachedThreadPool is not separate class its method ( newCachedThreadPool() ) .

CachedThreadPool Doc

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download