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多线程使用,及线程池配置的更多相关文章

  1. 玩转SpringBoot之定时任务@Scheduled线程池配置

    序言 对于定时任务,在SpringBoot中只需要使用@Scheduled 这个注解就能够满足需求,它的出现也给我们带了很大的方便,我们只要加上该注解,并且根据需求设置好就可以使用定时任务了. 但是, ...

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

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

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

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

  4. SpringBoot 线程池配置 实现AsyncConfigurer接口方法

      目的是:  通过实现AsyncConfigurer自定义线程池,包含异常处理  实现AsyncConfigurer接口对异常线程池更加细粒度的控制 *a) 创建线程自己的线程池  b) 对void ...

  5. TestNG的參数化測试、共享线程池配置、參数默认值配置

    在使用TestNG进行測试时,常常会使用到一些參数化配置,比方数据库.连接池.线程池数. 使用TestNG的參数@Parameter注解进行自己主动化读取 原创文章,版权全部.同意转载,标明出处:ht ...

  6. SpringBoot异步及线程池配置

    异步方法注解@Async 在SpringBoot中进行异步处理,可以使用异步注解@Async和@EnableAsync. @Async注解表示异步,如:@Async("asyncServic ...

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

    概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...

  8. Java多线程系列--“JUC线程池”02之 线程池原理(一)

    概要 在上一章"Java多线程系列--“JUC线程池”01之 线程池架构"中,我们了解了线程池的架构.线程池的实现类是ThreadPoolExecutor类.本章,我们通过分析Th ...

  9. Java多线程系列--“JUC线程池”03之 线程池原理(二)

    概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...

  10. Java多线程系列--“JUC线程池”04之 线程池原理(三)

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509960.html 本章介绍线程池的生命周期.在"Java多线程系列--“基础篇”01之 基 ...

随机推荐

  1. Java 多线程 破解密码 demo

    功能要求: 具体类: Decrypt  测试类,用来启动破解和日志线程 DecryptThread 破解线程类,用来生成测试的字符串,并暴力破解 LogThread 日志类,将输出每次生成的字符串结果 ...

  2. Sass常用写法

    Sass使用变量,变量以$开头 $bgcolor:#f40; background-color:$bgcolor; 如果变量需要嵌套在字符串当中,就需要写在#{}之中 $direction:left; ...

  3. Java 类引入 学习记录规整

    之前觉得声明一个类,再把另一个包内的声明数值用第一个类打印出来就可以了(加入引入包类) 结果发现是不对的 看了看demo  得出正确结果    ImportTest 被运行 引入下面的Import类 ...

  4. java的Date() 转换符

    本字段下均转自 csdn 阿念1989 本文是学习网络上的文章时的总结,感谢大家无私的分享. System.out.printf()方法可以对日期做处理输出. 对应列表 转换符 类型 举例 c 完整的 ...

  5. 【译】第2节--- 什么是Code First?

    原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF从EF4.1中引入了Code-Firs ...

  6. springmvc+druid+dataSource配置的两种方式

    一.一般的配置方式 数据库连接配置在jdbc.properties文件中,这种方式有一个最大的缺点,数据库的配置信息对开发人员是完全可见的,十分方便程序员删库跑路.spring配置具体如下: 1.jd ...

  7. React Native 之轮播图swiper组件

    注释:swiper组件是第三方组件 所以在使用之前应该先在命令行安装,然后将第三方的模块引入(第三方模块地址:https://github.com/leecade/react-native-swipe ...

  8. [BZOJ]|[Ural] Formula 1-----插头DP入门

    1519. Formula 1 Time limit: 1.0 secondMemory limit: 64 MB Background Regardless of the fact, that Vo ...

  9. hdu 1573 X问题 两两可能不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  10. 汇编语言调用Linux系统调用read和write

    .section .data UserMsg: .ascii "Please input the message:" LenOfUserMsg: .equ lenMsg, LenO ...