spring 配置 线程池并使用 springtest 进行测试
在 applicationContext.xml 中配置spring线程池:
<!-- 包路径扫描 -->
<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;
下面是 springtest 的实现方式:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class AsyncTask { @Autowired
private ThreadPoolTaskExecutor executor; // @PostConstruct 注解需要使用,否则不能成功看到输出(这里说的是在springtest里,在项目中使用不加这个,否则会报错)
@PostConstruct
public void testAsycTask() {
for (int i = 0; i < 10; i++) {
executor.execute(new Runnable() {
public void run() {
asyncTask();
}
});
}
} // 这里需要设置为 public
@Test
public void asyncTask() {
System.out.println("---" + Thread.currentThread().getName());
}
}
成功:

springmvc 每一个请求都会自动使用一个独立的线程来进行处理

spring 配置 线程池并使用 springtest 进行测试的更多相关文章
- java多线程、线程池及Spring配置线程池详解
1.java中为什么要使用多线程使用多线程,可以把一些大任务分解成多个小任务来执行,多个小任务之间互不影像,同时进行,这样,充分利用了cpu资源.2.java中简单的实现多线程的方式 继承Thread ...
- Spring Boot中如何配置线程池拒绝策略,妥善处理好溢出的任务
通过之前三篇关于Spring Boot异步任务实现的博文,我们分别学会了用@Async创建异步任务.为异步任务配置线程池.使用多个线程池隔离不同的异步任务.今天这篇,我们继续对上面的知识进行完善和优化 ...
- spring @Async 线程池使用
最近公司项目正逐渐从dubbo向springCloud转型,在本次新开发的需求中,全部使用springcloud进行,在使用时线程池,考虑使用spring封装的线程池,现将本次使用心得及内容记录下来 ...
- Spring Boot 线程池
参考 SpringBoot 线程池 程序猿DD-Spring Boot使用@Async实现异步调用:自定义线程池 如何优雅的使用和理解线程池 Spring Boot线程池的使用心得 博客园-Sprin ...
- Spring集成线程池
自己在程序中手动New很容易造成线程滥用,创建线程也是比较消耗资源的操作,所以建议如果有此需求,将线程池统一交给Spring框架进行管理. 如下: <!--Spring 集成线程池,不允许自己开 ...
- Spring配置连接池
---------------------siwuxie095 Spring 配置连接池 1.Spring 配置内置连接 ...
- Spring Boot 线程池的使用和扩展 - 转载
转载:http://blog.csdn.net/boling_cavalry/article/details/79120268 1.实战环境 windowns10: jdk1.8: springboo ...
- spring定时任务.线程池,自定义多线程配置
定时任务及多线程配置xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- spring boot 线程池配置
1.配置类 package cn.com.bonc.util; import java.util.concurrent.Executor; import java.util.concurrent.Th ...
随机推荐
- linux查看某IP尝试连接成功和失败次数
查看连接失败次数 cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2" ...
- WordCount系统分析与设计作业
Gitee项目地址 https://gitee.com/gitdq/homework psp表 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划 10 10 · ...
- python3.6和pip3安装
CenOS7 安装依赖环境 yum -y install openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc 编译 ...
- 一、Ajax 二、JSON数据格式 三、Ajax+Jquery 四、分页的实现
一.Ajax概述###<1>概述 ###<2>组成 以XMLHttpRequest为核心,发送Ajax请求和接收处理结果 以javascript为语言基础 以XML/JSON作 ...
- sql点滴—mysql中查询表的信息
mysql中查询表的信息 查询mysql表字段信息的sql语句 SHOW DATABASES //列出 MySQL Server 数据库. SHOW TABLES [FROM db_name] //列 ...
- 2.Dubbo2.5.3注册中心和监控中心部署
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.注册中心Zookeeper安装 (1)搭建要求 zk服务器集群规模不小于3个节点要求各服务器之间系统时间要 ...
- webstorm 2017 激活
参考:https://blog.csdn.net/wangyingwing/article/details/79119592
- JSONP方法解决跨域请求
Ajax跨域请求的问题 跨域:跨域名, 一个域名下的文件去请求了和他不一样的域名下的资源文件(注意是请求文件,而不是数据接口),那么就会产生跨域请求,下面来写一个ajax来跨域请求的例子 <!D ...
- AES块加密与解密
AES块加密与解密 解密目标 在CBC和CTR两种模式下分别给出十篇加密的样例密文,求解密一篇特定的密文 解密前提 全部密文及其加密使用的key都已给出 加密的方法遵循AES的标准 解密过程分析 实验 ...
- leetcode16—3 Sum Closet
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...