Threadpooltaskexecutor multithreaded use

Source: Internet
Author: User

1. Configuring the Threadpooltaskexecutor Bean

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 " >        <!--Scan Annotations -    <Context:component-scanBase-package= "com.qi.quartz">        <Context:exclude-filtertype= "annotation"expression= "org.springframework.stereotype.Controller" />    </Context:component-scan>        <BeanID= "taskexecutor"name= "taskexecutor"class= "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">          <!--number of core threads thread pool maintains minimum number of threads -        < propertyname= "corepoolsize"value= "ten" />          <!--the thread pool maintains idle time allowed by threads -        < propertyname= "keepaliveseconds"value= "$" />          <!--maximum number of threads maintained by the thread pool -        < propertyname= "maxpoolsize"value= " the" />          <!--buffer queue used by the thread pool -        < propertyname= "queuecapacity"value= "+" />         <!--The thread pool handles the policy of rejecting a task (a wireless thread is available), the Threadpoolexecutor.callerrunspolicy policy, the Caller's threads perform the task, and if the executor is closed, it is discarded.  -        < propertyname= "rejectedexecutionhandler">            <Beanclass= "java.util.concurrent.threadpoolexecutor$callerrunspolicy" />        </ property>     </Bean>      </Beans>

2.controller use

 packagecom.qi.quartz.web;Importjavax.annotation.Resource;Importorg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;Importorg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;Importorg.springframework.web.bind.annotation.ResponseBody, @Controller @requestmapping ("/test") public classThreadpoolexcutecontroller {Logger LOG= Loggerfactory.getlogger (threadpoolexcutecontroller.class); @Resource (name= "taskexecutor")    PrivateThreadpooltaskexecutor taskexecutor; @RequestMapping ("/execute") @ResponseBody public voidExecute () {taskexecutor.execute (NewRunnable () { public voidRun () {Try{log.info ("before the execution thread task begins"); Thread.CurrentThread (). Sleep (10000); if(log.isdebugenabled ()) {log.info ("execute Thread Task end"); }                } Catch(interruptedexception E) {e.printstacktrace ();    }            }                    }); }}

3. using Apache AB concurrency test

/usr/local/apache2/bin/ab-n 1000-c Http://192.168.8.101:8080/QuartzDemo/test/execute

Benchmarking 192.168.8.101 (be Patient)
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Finished 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)

We configured the core to handle 10 threads, a maximum of 20, a buffer queue of 100, a total time of 41.982, as we change these configurations, the processing situation is Different.

Change the configuration to

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 " >        <!--Scan Annotations -    <Context:component-scanBase-package= "com.qi.quartz">        <Context:exclude-filtertype= "annotation"expression= "org.springframework.stereotype.Controller" />    </Context:component-scan>        <BeanID= "taskexecutor"name= "taskexecutor"class= "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">          <!--number of core threads thread pool maintains minimum number of threads -        < propertyname= "corepoolsize"value= "+" />          <!--the thread pool maintains idle time allowed by threads -        < propertyname= "keepaliveseconds"value= "$" />          <!--maximum number of threads maintained by the thread pool -        < propertyname= "maxpoolsize"value= "+" />          <!--buffer queue used by the thread pool -        < propertyname= "queuecapacity"value= "$" />         <!--The thread pool handles the policy of rejecting a task (a wireless thread is available), the Threadpoolexecutor.callerrunspolicy policy, the Caller's threads perform the task, and if the executor is closed, it is discarded.  -        < propertyname= "rejectedexecutionhandler">            <Beanclass= "java.util.concurrent.threadpoolexecutor$callerrunspolicy" />        </ property>     </Bean>      </Beans>

Perform tests

./ab-n 1000-c Http://192.168.8.101:8080/QuartzDemo/test/execute

1000 requests, 1000 concurrent at a time

Results

Server software:apache-coyote/1.1Server Hostname:192.168.8.101Server Port:8080Document Path:/quartzdemo/test/executedocument Length:0bytesconcurrency level: +time taken forTests22.452Secondscomplete requests: +Failed requests:0Write errors:0Total Transferred:121121byteshtml transferred:0bytesrequests 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      the    3035processing: about 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) -%1529   the%11318   the%11630   the%11830   -%21315   the%22316  98%22338   about%22353  -%22421(longest Request)

It can be seen that it took only 22.452 seconds, but our requests were much higher 1000*1000-100*100 = 990000.

of course, as to how many threads to open, but also to see how the MACHINE.

Threadpooltaskexecutor multithreaded use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.