spring线程池的应用
2024-10-21 11:36:10
加载xml文件
在ApplicationContext.xml文件里面添加
- xmlns:task="http://www.springframework.org/schema/task"
xmlns:task="http://www.springframework.org/schema/task"
xmlns文件并且xsi:schemaLocation中添加
- http://www.springframework.org/schema/task
http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/task/spring-task.xsd
在spring中配置Executor
在ApplicationContext.xml文件里面添加
- <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 />
- <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添加
- maxOpenPreparedStatements=20
- removeAbandoned=true
- removeAbandonedTimeout=1800
- logAbandoned=true
- maxOpenPreparedStatements=20
- removeAbandoned=true
- removeAbandonedTimeout=1800
- logAbandoned=true
这是分别对线程池做配置
添加依赖注入
在所需要的service或者controller类里面添加
- @Resource(name = "taskExecutor")
- private TaskExecutor taskExecutor;
- @Resource(name = "taskExecutor")
- private TaskExecutor taskExecutor;
使用线程池进行并发操作
代码如下
- taskExecutor.execute(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- //要进行的并发操作
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- 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中声明变量如下
- @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;
- }
- @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;
最新文章
- spring初次体验
- Unknown tag
- YY游戏私有云平台实践 (转自InfoQ )
- flashbuilder发布release版本
- VC++制作DLL具体解释
- 如何在ASP.NET 项目中使用Silverlight页面
- Global build settings
- Oracle:grouping和rollup
- Smarterer Test
- linux下用tar命令将当前目录下文件按子目录压缩归档
- C语言中的函数指针
- 2017<;java技术>;预备作业计科冀浩然
- [LeetCode] 链表反转相关题目
- 19条MySQL优化准则
- StackExchange.Redis 异步超时解决方案
- 网站JS控制的QQ悬浮
- python绘图工具matplotlib在linux下安装和使用
- [skill] 异或
- mybatis源码解析7---MappedStatement初始化过程
- 10分钟看懂!基于Zookeeper的分布式锁
热门文章
- Python学习日记(二十三) 类命名空间和组合
- centos7 安装 git服务器
- java sql: PrepareStatement
- 微信公众号&;小程序 -- 获取并解密用户数据(获取openId、unionId)
- django获取数据queryset中的filter选项
- Linux 绑定 ttyUSBn 串口方法。
- Python3执行top指令
- 【独家】K8S漏洞报告 | CVE-2019-1002101解读
- k8s删除pod时,docker服务出现挂载点泄漏问题的解决
- java.lang.RuntimeException: org.springframework.dao.DuplicateKeyException: