Spring线程池ThreadPoolTaskExecutor配置及详情

Spring线程池ThreadPoolTaskExecutor配置及详情

1. ThreadPoolTaskExecutor配置

 <!-- spring thread pool executor -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 线程池维护线程的最少数量 -->
<property name="corePoolSize" value="5" />
<!-- 允许的空闲时间 -->
<property name="keepAliveSeconds" value="200" />
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="10" />
<!-- 缓存队列 -->
<property name="queueCapacity" value="20" />
<!-- 对拒绝task的处理策略 -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>

属性字段说明

corePoolSize:线程池维护线程的最少数量

keepAliveSeconds:允许的空闲时间

maxPoolSize:线程池维护线程的最大数量

queueCapacity:缓存队列

rejectedExecutionHandler:对拒绝task的处理策略

2. execute(Runable)方法执行过程

如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。

如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。

如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maxPoolSize,建新的线程来处理被添加的任务。

如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maxPoolSize,那么通过handler所指定的策略来处理此任务。也就是:处理任务的优先级为:核心线程corePoolSize、任务队列workQueue、最大线程 maximumPoolSize,如果三者都满了,使用handler处理被拒绝的任务。

当线程池中的线程数量大于corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止。这样,线程池可以动态的调整池中的线程数。

3. 示例代码

Junit Test

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MultiThreadConfig.class })
public class MultiThreadTest { @Autowired
private ThreadPoolTaskExecutor taskExecutor; @Autowired
private MultiThreadProcessService multiThreadProcessService; @Test
public void test() { int n = 20;
for (int i = 0; i < n; i++) {
taskExecutor.execute(new MultiThreadDemo(multiThreadProcessService));
System.out.println("int i is " + i + ", now threadpool active threads totalnum is " + taskExecutor.getActiveCount());
} try {
System.in.read();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

MultiThreadDemo

 /**
* 多线程并发处理demo
* @author daniel.zhao
*
*/
public class MultiThreadDemo implements Runnable { private MultiThreadProcessService multiThreadProcessService; public MultiThreadDemo() {
} public MultiThreadDemo(MultiThreadProcessService multiThreadProcessService) {
this.multiThreadProcessService = multiThreadProcessService;
} @Override
public void run() {
multiThreadProcessService.processSomething();
} }

MultiThreadProcessService

 @Service
public class MultiThreadProcessService { public static final Logger logger = Logger.getLogger(MultiThreadProcessService.class); /**
* 默认处理流程耗时1000ms
*/
public void processSomething() {
logger.debug("MultiThreadProcessService-processSomething" + Thread.currentThread() + "......start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
logger.debug("MultiThreadProcessService-processSomething" + Thread.currentThread() + "......end");
}
}

MultiThreadConfig

 @Configuration
@ComponentScan(basePackages = { "com.xxx.multithread" })
@ImportResource(value = { "classpath:config/application-task.xml" })
@EnableScheduling
public class MultiThreadConfig {
}
上一篇:MySQL MHA配置


下一篇:异步编程Promise/Deferred、多线程WebWorker