1. 通过XML定义bean
1 <!-- Spring线程池 -->
2 <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
3 <!-- 核心线程数 -->
4 <property name="corePoolSize" value="5" />
5 <!-- 线程池维护线程的最大数量 -->
6 <property name="maxPoolSize" value="10" />
7 <!-- 允许的空闲时间, 默认60秒 -->
8 <property name="keepAliveSeconds" value="60" />
9 <!-- 任务队列 -->
10 <property name="queueCapacity" value="50" />
11 <!-- 线程超过空闲时间限制,均会退出直到线程数量为0 -->
12 <property name="allowCoreThreadTimeOut" value="true"/>
13 <!-- 对拒绝task的处理策略 -->
14 <property name="rejectedExecutionHandler">
15 <bean class="java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy" />
16 </property>
17 </bean>
2. 使用task:executor方式
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:task="http://www.springframework.org/schema/task"
5 xsi:schemaLocation=
6 "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
8
9 <!-- 包路径扫描 -->
10 <context:component-scan base-package="spring.task"/>
11
12 <!-- 定义线程池 -->
13 <task:executor id="executor" pool-size="5" queue-capacity="10" rejection-policy="DISCARD_OLDEST"/>
14
15 </beans>