ThreadPoolTaskExecutor线程使用,及线程池配置
来源:互联网 发布:福州靠谱网络官网 编辑:程序博客网 时间:2024/06/07 07:06
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.1Server Hostname: 192.168.8.101Server Port: 8080Document Path: /QuartzDemo/test/executeDocument Length: 0 bytesConcurrency Level: 1000Time taken for tests: 22.452 secondsComplete requests: 1000Failed requests: 0Write errors: 0Total transferred: 121121 bytesHTML transferred: 0 bytesRequests 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] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 1 216 403.0 95 3035Processing: 210 6209 6834.3 1431 21534Waiting: 209 6208 6834.3 1431 21534Total: 334 6425 7071.3 1529 22421Percentage of the requests served within a certain time (ms) 50% 1529 66% 11318 75% 11630 80% 11830 90% 21315 95% 22316 98% 22338 99% 22353 100% 22421 (longest request)
可以看出仅用了22.452 秒,但是我们的请求数却高出了很多 1000*1000-100*100 = 990000。
当然了,至于开多少个线程,还要看机器如何了。
- ThreadPoolTaskExecutor线程使用,及线程池配置
- Spring线程池ThreadPoolTaskExecutor配置及详情
- Spring线程池ThreadPoolTaskExecutor配置及详情
- Spring线程池ThreadPoolTaskExecutor配置及详情
- ThreadPoolTaskExecutor线程池
- ThreadPoolTaskExecutor异步线程池
- Spring ThreadPoolTaskExecutor线程池
- ThreadPoolTaskExecutor异步线程池
- Spring线程池ThreadPoolTaskExecutor
- spring线程池ThreadPoolTaskExecutor
- spring线程池ThreadPoolTaskExecutor
- Spring线程池ThreadPoolTaskExecutor
- ThreadPoolTaskExecutor 创建线程池管理
- SPRING中的线程池ThreadPoolTaskExecutor
- ThreadPoolTaskExecutor 创建线程池管理
- SPRING中的线程池ThreadPoolTaskExecutor
- SPRING中的线程池ThreadPoolTaskExecutor
- SPRING中的线程池ThreadPoolTaskExecutor
- Java新手编程入门
- #jquery 和 animate 封装一个带动画的弹框
- [FAQ17975]Android M 首次开机不随sim卡自适应语言修改方案
- linux/android代码中对于不同项目中conf、dts、defconfig文件的处理方法
- 用R做中文LDA主题模型可视化分析
- ThreadPoolTaskExecutor线程使用,及线程池配置
- 2017.10.17离线赛总结
- java格式化日期的写法
- sublime text3 使用小技巧
- 第八周项目2-对称矩阵压缩存储的实现与应用-
- 史上最难的一道Java面试题 (分析篇)
- 文章标题
- 使用tensorflow中没有的模块(matplotlib),怎么办
- 20171018日记账流水