Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现。

Spring 配置线程池,有两种方式:

方式一:XML定义bean

    <!-- 包路径扫描 -->
<context:component-scan base-package="spring.task"/> <!-- Spring线程池 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="5" />
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="10" />
<!-- 允许的空闲时间, 默认60秒 -->
<property name="keepAliveSeconds" value="60" />
<!-- 任务队列 -->
<property name="queueCapacity" value="50" />
<!-- 线程超过空闲时间限制,均会退出直到线程数量为0 -->
<property name="allowCoreThreadTimeOut" value="true"/>
<!-- 对拒绝task的处理策略 -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy" />
</property>
</bean>
当一个新任务来临时:
1)如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务;
2)如果此时线程池中的数量等于corePoolSize,但是缓冲队列workQueue未满,那么任务被放入缓冲队列;
3)如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maxPoolSize,建新的线程来处理被添加的任务;
4)如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maxPoolSize,那么通过handler所指定的策略来处理此任务;
5)当线程池中的线程数量大于corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止,如果allowCoreThreadTimeOut为false,则线程数量维持在corePoolSize, 如果为true,则线程数量可最低降至0;

使用task:executor方式

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 包路径扫描 -->
<context:component-scan base-package="spring.task"/> <!-- 定义线程池 -->
<task:executor id="executor" pool-size="5" queue-capacity="10" rejection-policy="DISCARD_OLDEST"/> </beans>

测试代码:

@Component
public class AsyncTask { @Autowired
private ThreadPoolTaskExecutor executor; @PostConstruct
public void testAsycTask() {
for (int i = 0; i < 10; i++) {
executor.execute(new Runnable() {
public void run() {
asyncTask();
}
});
}
} private void asyncTask(){
System.out.println( "---" + Thread.currentThread().getName());
}
}

测试结果:

---executor-1
---executor-2
---executor-3
---executor-4
---executor-5
---executor-6
---executor-7
---executor-8
---executor-9
---executor-10

分享知识-快乐自己:Spring线程池配置的更多相关文章

  1. Spring线程池配置模板设计(基于Springboot)

    目录 线程池配置模板 基础的注解解释 常用配置参数 配置类设计 线程池使用 ThreadPoolTaskExecutor源码 线程池配置模板 springboot给我们提供了一个线程池的实现,它的底层 ...

  2. 007-多线程-JUC线程池-Spring线程池配置、池子如何配置参数

    一.概述 Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现. 1.1 ...

  3. spring线程池配置

    源自:http://zjriso.iteye.com/blog/771706 1.了解 TaskExecutor接口 Spring的TaskExecutor接口等同于java.util.concurr ...

  4. Spring线程池ThreadPoolTaskExecutor配置及详情

    Spring线程池ThreadPoolTaskExecutor配置及详情 1. ThreadPoolTaskExecutor配置 <!-- spring thread pool executor ...

  5. 【SSM Spring 线程池 OJ】 使用Spring线程池ThreadPoolTaskExecutor

    最近做的Online Judge项目,在本地判题的实现过程中,遇到了一些问题,包括多线程,http通信等等.现在完整记录如下: OJ有一个业务是: 用户在前端敲好代码,按下提交按钮发送一个判题请求给后 ...

  6. spring线程池的同步和异步(1)

    spring线程池(同步.异步) 一.spring异步线程池类图 二.简单介绍 2.1. TaskExecutor---Spring异步线程池的接口类,其实质是java.util.concurrent ...

  7. JDK线程池和Spring线程池的使用

    JDK线程池和Spring线程池实例,异步调用,可以直接使用 (1)JDK线程池的使用,此处采用单例的方式提供,见示例: public class ThreadPoolUtil { private s ...

  8. TestNg线程池配置、执行次数配置、超时配置

    使用注解的方式对TestNg线程池配置.执行次数配置.超时配置 注:使用注解来控制测试方法运行的次数和超时时间,timeOut在单线程或者多线程模式下都可用,threadPoolSize设置了线程池的 ...

  9. Spring线程池开发实战

    Spring线程池开发实战 作者:chszs,转载需注明. 作者博客主页:http://blog.csdn.net/chszs 本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然, ...

随机推荐

  1. ehcache 常用配置项详解(三)

    EhCache 给我们提供了丰富的配置来配置缓存的设置: 这里列出一些常见的配置项: cache元素的属性: name:缓存名称 maxElementsInMemory:内存中最大缓存对象数 maxE ...

  2. PAT 1069. 微博转发抽奖(20)

    小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...

  3. Longest Common Prefix -最长公共前缀

    问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...

  4. 经典书Discrete.Mathematics上的大神

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  5. 找出n的阶乘末尾有几个零

    原理:因为10由2*5组成,而构成2的因数比5多 所以最终转换成求5的个数 int getNumber(int n) { int count = 0; while(n) { n = n/5; coun ...

  6. 【jenkins】jenkins实时显示python脚本输出

    jenkins在构建shell脚本时可以实时输出结果,但是在构建python脚本时,是等到python执行完成以后,才显示结果,这个对于我们判断脚本执行状态非常不利 这里介绍一种方法,能够实时显示py ...

  7. ORA-00001:unique constraint violated 以及 Incorrect result size: expected 1, actual 0

    往数据库中插入数据时报错:   www.2cto.com   ORA-00001: unique constraint (IDX_CARTON_HEADER)violated.   即往CARTON_ ...

  8. PAT 天梯赛 L1-029. 是不是太胖了 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-029 AC代码 #include <iostream> #include <cstdio&g ...

  9. 【CodeChef】Small factorials(BigInteger笔记)

    You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t&l ...

  10. Python 数值类型

    1.数值类型分为整形(二进制(0b),八进制(0o),十进制,十六进制(0x) ),浮点型,long,complex(复合行) 当我们说十进制数的时候,是逢10进1,就是说到达10的时候就要向前一位进 ...