ThreadPoolTaskExecutor多线程使用,及线程池配置
1.配置 ThreadPoolTaskExecutor bean
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描注解 -->
<context:component-scan base-package="com.qi.quartz">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> <bean id="taskExecutor" name="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 线程池维护线程的最少数量 -->
<property name="corePoolSize" value="10" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="200" />
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="20" />
<!-- 线程池所使用的缓冲队列 -->
<property name="queueCapacity" value="100" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃. -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean> </beans>
2.controller使用
package com.qi.quartz.web; import javax.annotation.Resource; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/test")
public class ThreadPoolExcuteController { Logger LOG = LoggerFactory.getLogger(ThreadPoolExcuteController.class); @Resource(name = "taskExecutor")
private ThreadPoolTaskExecutor taskExecutor; @RequestMapping("/execute")
@ResponseBody
public void execute(){
taskExecutor.execute(new Runnable(){ public void run() {
try {
LOG.info("执行线程任务开始前");
Thread.currentThread().sleep(10000);
if (LOG.isDebugEnabled()) {
LOG.info("执行线程任务结束");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} });
} }
3.使用 apache ab 并发测试
/usr/local/apache2/bin/ab -n 1000 -c 1000 http://192.168.8.101:8080/QuartzDemo/test/execute
Benchmarking 192.168.8.101 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: Apache-Coyote/1.1
Server Hostname: 192.168.8.101
Server Port: 8080
Document Path: /QuartzDemo/test/execute
Document Length: 3 bytes
Concurrency Level: 1000
Time taken for tests: 41.982 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 163000 bytes
HTML transferred: 3000 bytes
Requests per second: 23.82 [#/sec] (mean)
Time per request: 41982.345 [ms] (mean)
Time per request: 41.982 [ms] (mean, across all concurrent requests)
Transfer rate: 3.79 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 304 211.4 291 1077
Processing: 172 22968 13412.1 21237 41240
Waiting: 161 22900 13455.0 21174 41171
Total: 472 23272 13441.8 21505 41944
Percentage of the requests served within a certain time (ms)
50% 21505
66% 31398
75% 31725
80% 40963
90% 41467
95% 41605
98% 41930
99% 41939
100% 41944 (longest request)
我们配置的核心处理10个线程,最大20个,缓冲队列100,总耗时41.982,随着我们更改这些配置的时候,处理的情况就不同了。
更改配置为
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描注解 -->
<context:component-scan base-package="com.qi.quartz">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> <bean id="taskExecutor" name="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 线程池维护线程的最少数量 -->
<property name="corePoolSize" value="100" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="200" />
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="100" />
<!-- 线程池所使用的缓冲队列 -->
<property name="queueCapacity" value="500" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃. -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean> </beans>
执行测试
./ab -n 1000 -c 1000 http://192.168.8.101:8080/QuartzDemo/test/execute
1000个请求,每次 1000个并发
结果
Server Software: Apache-Coyote/1.1
Server Hostname: 192.168.8.101
Server Port: Document Path: /QuartzDemo/test/execute
Document Length: bytes Concurrency Level:
Time taken for tests: 22.452 seconds
Complete requests:
Failed requests:
Write errors:
Total transferred: bytes
HTML transferred: bytes
Requests per second: 44.54 [#/sec] (mean)
Time per request: 22452.351 [ms] (mean)
Time per request: 22.452 [ms] (mean, across all concurrent requests)
Transfer rate: 5.27 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 403.0
Processing: 6834.3
Waiting: 6834.3
Total: 7071.3 Percentage of the requests served within a certain time (ms)
%
%
%
%
%
%
%
%
% (longest request)
可以看出仅用了22.452 秒,但是我们的请求数却高出了很多 1000*1000-100*100 = 990000。
当然了,至于开多少个线程,还要看机器如何了。
ThreadPoolTaskExecutor多线程使用,及线程池配置的更多相关文章
- 玩转SpringBoot之定时任务@Scheduled线程池配置
序言 对于定时任务,在SpringBoot中只需要使用@Scheduled 这个注解就能够满足需求,它的出现也给我们带了很大的方便,我们只要加上该注解,并且根据需求设置好就可以使用定时任务了. 但是, ...
- TestNg线程池配置、执行次数配置、超时配置
使用注解的方式对TestNg线程池配置.执行次数配置.超时配置 注:使用注解来控制测试方法运行的次数和超时时间,timeOut在单线程或者多线程模式下都可用,threadPoolSize设置了线程池的 ...
- Spring线程池配置模板设计(基于Springboot)
目录 线程池配置模板 基础的注解解释 常用配置参数 配置类设计 线程池使用 ThreadPoolTaskExecutor源码 线程池配置模板 springboot给我们提供了一个线程池的实现,它的底层 ...
- SpringBoot 线程池配置 实现AsyncConfigurer接口方法
目的是: 通过实现AsyncConfigurer自定义线程池,包含异常处理 实现AsyncConfigurer接口对异常线程池更加细粒度的控制 *a) 创建线程自己的线程池 b) 对void ...
- TestNG的參数化測试、共享线程池配置、參数默认值配置
在使用TestNG进行測试时,常常会使用到一些參数化配置,比方数据库.连接池.线程池数. 使用TestNG的參数@Parameter注解进行自己主动化读取 原创文章,版权全部.同意转载,标明出处:ht ...
- SpringBoot异步及线程池配置
异步方法注解@Async 在SpringBoot中进行异步处理,可以使用异步注解@Async和@EnableAsync. @Async注解表示异步,如:@Async("asyncServic ...
- Java多线程系列--“JUC线程池”06之 Callable和Future
概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...
- Java多线程系列--“JUC线程池”02之 线程池原理(一)
概要 在上一章"Java多线程系列--“JUC线程池”01之 线程池架构"中,我们了解了线程池的架构.线程池的实现类是ThreadPoolExecutor类.本章,我们通过分析Th ...
- Java多线程系列--“JUC线程池”03之 线程池原理(二)
概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...
- Java多线程系列--“JUC线程池”04之 线程池原理(三)
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509960.html 本章介绍线程池的生命周期.在"Java多线程系列--“基础篇”01之 基 ...
随机推荐
- 改变checkbox样式问题
选择1 选择2 选择3 选择4 选择5 <form action=""> <label for="test">选择1 <inp ...
- UVa 11300 分金币
https://vjudge.net/problem/UVA-11300 题意: 圆桌上有n个人,每个人都有一定的初始金币,每个人可以给他旁边的人一些金币,最终使每个人的金币数相等.计算最少需要转手的 ...
- c# 、 Asp.net 获取本地IP和MAC地址
using System; using System.Management; using System.Net; public class Program { static void Main(str ...
- go下载安装
https://studygolang.com/dl 一直下一步即可.
- linux 系统调用号表
位于 /usr/include/asm/unistd.h 由于我是64位系统,所以有一些额外的东西.我的这个文件为下文 #ifndef _ASM_X86_UNISTD_H #define _ASM_X ...
- Qt5.3.2openglVS2010_QSqlField_字段类型
1.本来想通过 QSqlField::typeID() 来找字段类型,但是没找到... 然而看到了 SQL_INTEGER.SQL_SMALLINT等的使用(在“static QVariant::Ty ...
- php 四种基础排序
1. 冒泡排序算法 * 思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来. * 比如:2,4,1 // 第一次 冒出的泡是4 * ...
- 原生 js 时钟实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 雷林鹏分享:C# 类(Class)
C# 类(Class) 当您定义一个类时,您定义了一个数据类型的蓝图.这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作.对象是类的实 ...
- android--------Dagger2介绍与简单使用(一)
1:Dagger2是啥 Dagger是为Android和Java平台提供的一个完全静态的,在编译时进行依赖注入的框架,原来是由Square公司维护的然后现在把这堆东西扔给Google维护了. 一般的I ...