- 定义taskExecutor bean
<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>
2.代码中注入taskExecutor
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
- 业务代码中使用
try {
taskExecutor.execute(new Runnable() {
@Override
public void run() {
logger.info("save push log ......");
jcXtZnxxMapper.insert(jcXtZnxx);
}
});
} catch (Exception e) {
e.printStackTrace();
}