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.xsdhttp://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")@ResponseBodypublic 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.xsdhttp://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:            8080Document Path:          /QuartzDemo/test/execute
Document Length:        0 bytesConcurrency Level:      1000
Time taken for tests:   22.452 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      121121 bytes
HTML transferred:       0 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] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        1  216 403.0     95    3035
Processing:   210 6209 6834.3   1431   21534
Waiting:      209 6208 6834.3   1431   21534
Total:        334 6425 7071.3   1529   22421Percentage of the requests served within a certain time (ms)50%   152966%  1131875%  1163080%  1183090%  2131595%  2231698%  2233899%  22353100%  22421 (longest request)

可以看出仅用了22.452 秒,但是我们的请求数却高出了很多  1000*1000-100*100 = 990000。

当然了,至于开多少个线程,还要看机器如何了。

转载于:https://www.cnblogs.com/yun965861480/p/6517000.html

ThreadPoolTaskExecutor多线程使用,及线程池配置相关推荐

  1. Java多线程之线程池配置合理线程数

    Java多线程之线程池配置合理线程数 目录 代码查看公司服务器或阿里云是几核的 合理线程数配置之CPU密集型 合理线程数配置之IO密集型 1. 代码查看公司服务器或阿里云是几核的 要合理配置线程数首先 ...

  2. Springboot应用中线程池配置教程(2021版)

    前言:日常开发中我们常用ThreadPoolExecutor提供的线程池服务帮我们管理线程,在Springboot中更是提供了@Async注解来简化业务逻辑提交到线程池中执行的过程.由于Springb ...

  3. linux下c语言线程传参数,【linux】C语言多线程中运行线程池,在线程池中运行线程池,,传递的结构体参数值为空/NULL/0...

    C语言多线程中运行线程池,在线程池中运行线程池,,传递的结构体参数值为空/NULL/0 本贴问题,之前已经提问过一次,当时已经解决了,原贴在这里https://segmentfault.com/q/1 ...

  4. 【ThreadPoolTaskExecutor】 SpringBoot 的线程池的使用

    一.配置 ThreadPoolTaskExecutor 创建一个文件夹 config ,新建一个类 ThreadPoolConfig.java import org.springframework.c ...

  5. 若依专题 线程池配置

    若依项目 | 线程池配置 概括: ruoyi项目中的线程池配置以@bean的方式自定义ThreadPoolTaskExecutor对象.ScheduledExecutorService对象放入Spri ...

  6. 解决URL存在特殊符号、异步线程池配置、动态加载lib下所有jar包

    一.解决URL存在特殊符号|{}?&.URL中包含%2F.URL中包含%5C import lombok.extern.slf4j.Slf4j; import org.springframew ...

  7. 什么?用@Async会内存溢出?看看你的线程池配置了没!

    上一篇我们介绍了如何使用@Async注解来创建异步任务,我可以用这种方法来实现一些并发操作,以加速任务的执行效率.但是,如果只是如前文那样直接简单的创建来使用,可能还是会碰到一些问题.存在有什么问题呢 ...

  8. Java多线程系列--“JUC线程池”06之 Callable和Future

    转载自  Java多线程系列--"JUC线程池"06之 Callable和Future Callable 和 Future 简介 Callable 和 Future 是比较有趣的一 ...

  9. spring定时任务Scheduled与定时任务线程池配置SchedulingConfigurer ,Java

    spring定时任务Scheduled与定时任务线程池配置SchedulingConfigurer ,Java spring默认定时任务的使用 package zhangphil.demo;impor ...

最新文章

  1. Python isinstance() 函数
  2. dbgridview内操作粘贴,复制,等量复制,增量复制
  3. 实例——在编程过程中进行单元测试
  4. Windows 8 页面应用测试(2)
  5. 01.使用File类读写文件
  6. 【干货来了】ComponentOne经典在线演示等你来体验!(上)
  7. 用Java写一个递归遍历目录下面的所有文件
  8. 计算机终端的串口并口指什么,串并口
  9. ArcGIS使用模型构建器批量剪裁影像
  10. BugkuCTF:散乱的密文,凯撒部长的奖励,一段base64
  11. U盘插入电脑需要格式化,数据恢复
  12. 咸鱼 转转,苹果产品真伪验证
  13. deficit记忆_总算懂得单词every分析记忆方法
  14. 华为鸿蒙系统怎么安装软件,华为鸿蒙系统2.0怎么进行安装?鸿蒙系统2.0安装步骤一览...
  15. 原厂对NPI安全稽核要求
  16. 知物由学 | 再造巴别塔,我们如何进行NLP跨语言知识迁移?
  17. 电信网络性能质量测量
  18. C++ STL函数库 stack (henu.hjy)
  19. 大型ECShop安装搬家升级错误问题最全攻略
  20. 【红宝书】OpenGL Demo code编译与运行

热门文章

  1. 不确定mysql是否安装成功了怎么办
  2. 「 每日一练,快乐水题 」682. 棒球比赛
  3. 【快乐水题】686. 重复叠加字符串匹配
  4. S5PV210开发 -- 驱动开发相关硬件简介
  5. 数据结构:表达式之中缀转后缀
  6. 【译】Three Security Trends Are Key to Decentralize Artificial Intelligence
  7. 美团外卖骑手背后的AI技术
  8. python︱HTML网页解析BeautifulSoup学习笔记
  9. Binder fuzz安全研究
  10. 一起学设计模式-策略模式