Currently, we have 2 thread pools in one of our apps:
- The first one is the one used to handle scheduled tasks
- The second deals with parallel processing of each scheduled task being run
The need to set up two different pools came from the following thinking: if several scheduled tasks are queued up in the main (first) pool and it triggers it's subtasks (parallel processing) in the same pool this would result in a race condition as would also get queued "behind" the other scheduled tasks, so nothing would actually end and deadlock would occur.
What if subtasks have higher priority than scheduled tasks? Would they "jump" the queue and pause scheduled tasks in order to finish? Or that won't happen? Is there some way to force such behavior? Or tasks can't be paused when an ThreadPoolExecutor is already running them?
Pool 1 is defined in Spring's application context XML config file as:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="cl.waypoint.mailer.reportes" />
<task:annotation-driven scheduler="myScheduler" />
<task:scheduler id="myScheduler" pool-size="2" />
<aop:aspectj-autoproxy />
</beans>
Pool 2 is defined in code as follows:
public static ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 30, TimeUnit.SECONDS,
new LinkedBlockingDeque<Runnable>(), new ThreadFactory() {
final AtomicLong count = new AtomicLong(0);
private String namePreffix = "TempAndDoor";
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(false);
t.setPriority(Thread.NORM_PRIORITY);
t.setName(MessageFormat.format("{0}-{1}", namePreffix, count.getAndIncrement()));
return t;
}
});