1、Sping配置文件

<!-- 线程池配置 -->
<bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="10" />
<!-- 最大线程数 -->
<property name="maxPoolSize" value="50" />
<!-- 队列最大长度 -->
<property name="queueCapacity" value="1000" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="300" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>

2、定义任务类

 package com.syj.phis.tools.test;

 import java.io.Serializable;
import java.util.concurrent.Callable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import com.syj.phis.ehr.entity.EhrBase;
import com.syj.phis.ehr.service.EhrBaseService;
/**
* 获取个人档案的任务类:
*
* 1.将任务类所需要的参数,声明为全局变量,并提供set方法.
* 2.实现Callable接口(Callable接口支持返回值,而Runnable接口不支持),并重写其call方法,将要业务代码写在call方法中.
*
* @author shiyanjun
*/
@Component
@Scope("prototype")
public class EhrDownloadTask implements Callable<EhrBase>, Serializable { private static final long serialVersionUID = -6626027616177700489L; @Autowired
private EhrBaseService ehrBaseService;
private String ehrId; public void setEhrId(String ehrId) {
this.ehrId = ehrId;
} /**
* 根据ehrId获取档案
*/
public EhrBase call() throws Exception { //打印当前线程的名称
System.out.println(Thread.currentThread().getName() + "....");
return (EhrBase) ehrBaseService.findByEhrId(ehrId);
}
}

3、测试

 package com.syj.phis.tools.test;

 import java.util.concurrent.Future;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.syj.phis.ehr.entity.EhrBase;
/**
* 多线程任务测试类:
*
* 1.使用Spring提供的线程池ThreadPoolTaskExecutor执行线程任务.
* 2.通过set方法传递参数.
* 3.使用Future对象封装返回值.
* 4.将每一个任务类使用@Autowired注解,交给Spring管理.
*
* @author shiyanjun
*/
@Controller
@RequestMapping(value = "/thread/pool/test")
public class ThreadPoolController {
@Autowired
private ThreadPoolTaskExecutor poolTaskExecutor;
@Autowired
private EhrDownloadTask ehrDownloadTask; /**
* 根据ehrId获取档案
* 请求路径示例:http://localhost:8080/phis/app/thread/pool/test/ehr/5065a1f1-c990-47f5-a58b-dd8fb240c215
* @param ehrId
* @return
*/
@RequestMapping(value = "ehr/{ehrId}", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public EhrBase download(@PathVariable("ehrId") String ehrId){ ehrDownloadTask.setEhrId(ehrId); //将任务交给Spring的线程任务执行器处理
Future<EhrBase> future = poolTaskExecutor.submit(ehrDownloadTask);
try {
//获取返回值
return future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Spring的线程池ThreadPoolTaskExecutor使用案例的更多相关文章

  1. spring boot: 线程池ThreadPoolTaskExecutor, 多线程

    由于项目里需要用到线程池来提高处理速度,记录一下spring的taskExecutor执行器来实现线程池. ThreadPoolTaskExecutor的配置在网上找了很多解释没找到,看了下Threa ...

  2. Spring线程池ThreadPoolTaskExecutor配置及详情

    Spring线程池ThreadPoolTaskExecutor配置及详情 1. ThreadPoolTaskExecutor配置 <!-- spring thread pool executor ...

  3. spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue

    一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...

  4. 【SSM Spring 线程池 OJ】 使用Spring线程池ThreadPoolTaskExecutor

    最近做的Online Judge项目,在本地判题的实现过程中,遇到了一些问题,包括多线程,http通信等等.现在完整记录如下: OJ有一个业务是: 用户在前端敲好代码,按下提交按钮发送一个判题请求给后 ...

  5. Spring集成线程池

    自己在程序中手动New很容易造成线程滥用,创建线程也是比较消耗资源的操作,所以建议如果有此需求,将线程池统一交给Spring框架进行管理. 如下: <!--Spring 集成线程池,不允许自己开 ...

  6. spring @Async 线程池使用

    最近公司项目正逐渐从dubbo向springCloud转型,在本次新开发的需求中,全部使用springcloud进行,在使用时线程池,考虑使用spring封装的线程池,现将本次使用心得及内容记录下来 ...

  7. Spring Boot 线程池

    参考 SpringBoot 线程池 程序猿DD-Spring Boot使用@Async实现异步调用:自定义线程池 如何优雅的使用和理解线程池 Spring Boot线程池的使用心得 博客园-Sprin ...

  8. Spring中的线程池ThreadPoolTaskExecutor介绍

    前言: Java SE 5.0引入了ThreadPoolExecutor.ScheduledThreadPoolExecutor.Spring 2.x借助ConcurrentTaskExecutor和 ...

  9. SPRING中的线程池ThreadPoolTaskExecutor(转)

    转自:https://blog.csdn.net/zhanglongfei_test/article/details/51888433 一.初始化 1,直接调用 ThreadPoolTaskExecu ...

随机推荐

  1. Unity脚本在层级面板中的执行顺序测试3

    断断续续的写了3篇,以后有时间可以做成一个系列了 前面2篇测试了GameObject的顺序,以及Awake和OnEnable的时机: Unity脚本在层级面板中的执行顺序测试1 http://www. ...

  2. 网页撤销后ubuntu本地撤销

    git reset --hard <commit_id> 回到上一个commit: 之后再    git reset --hard :

  3. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  4. Android各种获取Context方法

    首先讲一讲这四个函数的区别,后面还有我对context的一些理解区别如下所示: 原文链接http://stackoverflow.com/questions/6854265/getapplicatio ...

  5. iOS- 详解文本属性Attributes

    1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...

  6. 苹果Xcode帮助文档阅读指南

    文档导读 https://developer.apple.com/legacy/library/navigation/ 前面我们讲Xcode的文档结构是在介绍如何能够快速定位到你要找的内容.但是很多人 ...

  7. 配置 ASP.NET Linux( CentOS 6.5 ) 运行环境 MONO + Jexus

    1.更新系统 在命令行下执行 yum –y update 2.安装必要的软件 yum -y install gcc gcc-c++ bison pkgconfig glib2-devel gettex ...

  8. qt 2 打开文件选择框

    QString sFileName = ""; sFileName = QFileDialog::getOpenFileName(this,tr("Open") ...

  9. BZOJ 1822 Frozen Nova 冷冻波(最大流)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1822 题意:WJJ喜欢“魔兽争霸”这个游戏.在 游戏中,巫妖是一种强大的英雄,它的技能F ...

  10. VMware ESXI5.0的安装配置 zz

    http://www.hotxf.com/thread-297-1-1.html 1,    Vmware ESXI 光盘一张文件大小290M,本教程是以 5.0为案例. 2,    所需要安装的操作 ...