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. BZOJ3942: [Usaco2015 Feb]Censoring 栈+KMP

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

  2. Spring报NoSuchBeanDefinitionException

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 上述可以看出Ac ...

  3. MVC ---- EF的延迟加载

    //EF中的where 有延迟加载功能(Iqueryable中的where) Sys_Log pEdit = nb.Sys_Log.Where(p=>p.F_Account== "su ...

  4. python ros 回充调用demo

    #!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String def talker(): pub = ...

  5. HTML5-form表单

    什么是表单? 01.获取用户的输入  ==>收集数据 02.将用户的输入发送到服务器   ==>与服务器进行交互 相关属性: action:我们收集完用户的信息之后,需要提交的服务器地址 ...

  6. Linux更改主机名

    1.临时 # hostname newhostname 2.修改/etc/hostname文件,需重启 # vim /etc/hostname 3.查看 # hostname Ubuntu18 # h ...

  7. 字符集(编码)转换_Windows

    ZC: 来自 我的项目 czgj ZC: (1).经过测试 MultiByteToWideChar(...) 返回的是 (需要的)WideChar[宽字符]的个数:(2).WideCharToMult ...

  8. [原][osg][osgEarth]osg::Matrix 父子节点的变化关系

    //osg::Matrix offsetmatrix 计算出子节点在父节点下的绝对坐标 //osg::Matrix offposition 用来计算当前节点相对父节点的位置 osg::Matrix o ...

  9. EM算法及其推广

    概述 EM算法是一种迭代算法,用于含有隐变量(hidden variable)的概率模型参数的极大似然估计,或极大后验概率估计. EM算法的每次迭代由两步组成:E步,求期望(expectation): ...

  10. c++ primer plus 第五章 课后题答案

    #include <iostream> using namespace std; int main() { ; cout << "Please enter two n ...