Spring--springmvc配线程池Executor做多线程并发操作



加载xml文件

在ApplicationContext.xml文件里面添加
  1. xmlns:task="http://www.springframework.org/schema/task"  
xmlns:task="http://www.springframework.org/schema/task"
xmlns文件并且xsi:schemaLocation中添加
  1. http://www.springframework.org/schema/task   
http://www.springframework.org/schema/task 
  1. http://www.springframework.org/schema/task/spring-task.xsd  
http://www.springframework.org/schema/task/spring-task.xsd



在spring中配置Executor

在ApplicationContext.xml文件里面添加
  1. <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
  2.         <!-- 核心线程数 -->       
  3.         <property name="corePoolSize" value="${task.core_pool_size}" />  
  4.         <!-- 最大线程数 -->    
  5.         <property name="maxPoolSize" value="${task.max_pool_size}" />  
  6.         <!-- 队列最大长度 -->  
  7.         <property name="queueCapacity" value="${task.queue_capacity}" />  
  8.         <!-- 线程池维护线程所允许的空闲时间,默认为60s -->  
  9.         <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />  
  10.     </bean>  
  11.     <!-- 注解式 -->  
  12.     <task:annotation-driven />  
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 核心线程数 -->     
        <property name="corePoolSize" value="${task.core_pool_size}" />
        <!-- 最大线程数 -->  
        <property name="maxPoolSize" value="${task.max_pool_size}" />
        <!-- 队列最大长度 -->
        <property name="queueCapacity" value="${task.queue_capacity}" />
        <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
        <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />
    </bean>
    <!-- 注解式 -->
    <task:annotation-driven />
在dbconfig.properties添加
  1. maxOpenPreparedStatements=20  
  2. removeAbandoned=true  
  3. removeAbandonedTimeout=1800  
  4. logAbandoned=true  
maxOpenPreparedStatements=20
removeAbandoned=true
removeAbandonedTimeout=1800
logAbandoned=true
这是分别对线程池做配置

添加依赖注入

在所需要的service或者controller类里面添加
  1. @Resource(name = "taskExecutor")  
  2. private TaskExecutor taskExecutor;  
@Resource(name = "taskExecutor")
private TaskExecutor taskExecutor;

使用线程池进行并发操作

代码如下
  1. taskExecutor.execute(new Runnable() {  
  2.   
  3.     @Override  
  4.     public void run() {  
  5.         // TODO Auto-generated method stub  
  6.         try {  
  7.                                        //要进行的并发操作  
  8.         } catch (Exception e) {  
  9.             // TODO Auto-generated catch block  
  10.             e.printStackTrace();  
  11.         }  
  12.     }  
  13. });  
			taskExecutor.execute(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
                                          //要进行的并发操作
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			




提示

注意在线程中操作变量时候变量的作用域范围。需要在这个controller或者sevice中声明变量如下

  1. @Controller  
  2. public class IndexController {  
  3. int studentscount = 0;  
  4. @RequestMapping(value = "/index.html")  
  5.     public ModelAndView goIndex() {  
  6.         logBefore(logger, "列表Center");  
  7.         ModelAndView mv = this.getModelAndView();  
  8.                 taskExecutor.execute(new Runnable() {  
  9.   
  10.                 @Override  
  11.                 public void run() {  
  12.                     // TODO Auto-generated method stub  
  13.                     // 得到所有学生人数  
  14.                     try {  
  15.                                           studentscount = coursesService.getStudentCount(pd);  
  16.                     } catch (Exception e) {  
  17.                         // TODO Auto-generated catch block  
  18.                         e.printStackTrace();  
  19.                     }  
  20.   
  21.                 }  
  22.             });  
  23.                   mv.addObject("studentscount", studentscount);  
  24.                   mv.setViewName("common/index");  
  25.                   return mv;  
  26. }  
@Controller
public class IndexController {
int studentscount = 0;
@RequestMapping(value = "/index.html")
	public ModelAndView goIndex() {
		logBefore(logger, "列表Center");
		ModelAndView mv = this.getModelAndView();
                taskExecutor.execute(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					// 得到所有学生人数
					try {
                                          studentscount = coursesService.getStudentCount(pd);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			});
                  mv.addObject("studentscount", studentscount);
                  mv.setViewName("common/index");
                  return mv;
}
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值