加载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 />
  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 />

在dbconfig.properties添加

  1. maxOpenPreparedStatements=20
  2. removeAbandoned=true
  3. removeAbandonedTimeout=1800
  4. logAbandoned=true
  1.  
    maxOpenPreparedStatements=20
  2.  
    removeAbandoned=true
  3.  
    removeAbandonedTimeout=1800
  4.  
    logAbandoned=true

这是分别对线程池做配置

 

添加依赖注入

在所需要的service或者controller类里面添加

  1. @Resource(name = "taskExecutor")
  2. private TaskExecutor taskExecutor;
  1.  
    @Resource(name = "taskExecutor")
  2.  
    private TaskExecutor taskExecutor;

使用线程池进行并发操作

代码如下

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

 

提示

注意在线程中操作变量时候变量的作用域范围。需要在这个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. @Override
  10. public void run() {
  11. // TODO Auto-generated method stub
  12. // 得到所有学生人数
  13. try {
  14. studentscount = coursesService.getStudentCount(pd);
  15. } catch (Exception e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. }
  20. });
  21. mv.addObject("studentscount", studentscount);
  22. mv.setViewName("common/index");
  23. return mv;
    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.  

最新文章

  1. spring初次体验
  2. Unknown tag
  3. YY游戏私有云平台实践 (转自InfoQ )
  4. flashbuilder发布release版本
  5. VC++制作DLL具体解释
  6. 如何在ASP.NET 项目中使用Silverlight页面
  7. Global build settings
  8. Oracle:grouping和rollup
  9. Smarterer Test
  10. linux下用tar命令将当前目录下文件按子目录压缩归档
  11. C语言中的函数指针
  12. 2017&lt;java技术&gt;预备作业计科冀浩然
  13. [LeetCode] 链表反转相关题目
  14. 19条MySQL优化准则
  15. StackExchange.Redis 异步超时解决方案
  16. 网站JS控制的QQ悬浮
  17. python绘图工具matplotlib在linux下安装和使用
  18. [skill] 异或
  19. mybatis源码解析7---MappedStatement初始化过程
  20. 10分钟看懂!基于Zookeeper的分布式锁

热门文章

  1. Python学习日记(二十三) 类命名空间和组合
  2. centos7 安装 git服务器
  3. java sql: PrepareStatement
  4. 微信公众号&amp;小程序 -- 获取并解密用户数据(获取openId、unionId)
  5. django获取数据queryset中的filter选项
  6. Linux 绑定 ttyUSBn 串口方法。
  7. Python3执行top指令
  8. 【独家】K8S漏洞报告 | CVE-2019-1002101解读
  9. k8s删除pod时,docker服务出现挂载点泄漏问题的解决
  10. java.lang.RuntimeException: org.springframework.dao.DuplicateKeyException: