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. jquery闭包的使用

    <div id="divTest"> Test </div> <br /> <hr /> <div id="divT ...

  2. JAVA fundamentals of exception handling mechanism

    Agenda Three Categories Of Exceptions Exceptions Hierarchy try-catch-finally block The try-with-reso ...

  3. yum报错

    用man clean all man yum,可以看到,clean选项的作用是: Is  used  to clean up various things which accumulate in th ...

  4. 嵌入式学习_AD学习篇

    AD基础使用: 1.建立一个工作区 (.DsnWrk) workspace 2.建立一个PCB工程(.PrjPCB)  project 3.建立一个PCB原理图文件(理论上告诉你两个点连接起来) 建立 ...

  5. 国内下Android源码地址

    1 Android 4.4 with kernel: http://pan.baidu.com/s/1bnuDtHt 下载后,请务必阅读 必读.txt 2 android 5.0源码下载 http:/ ...

  6. Spring 框架 详解 (二)

    Spring的入门的程序: 1.1.1 下载Spring的开发包: spring-framework-3.2.0.RELEASE-dist.zip ---Spring开发包 * docs :sprin ...

  7. RabbitMQ在CentOS上的简单安装配置

    安装 1.依赖Erlang,yum install erlang安装之 2.去官网下载Fedora/RHEL的rpm包,rpm -ivh rabbitmq-server-*.noarch.rpm 安装 ...

  8. Java_JDK_HashMap

    (二)HashMap 需要注意的无非几点: 是什么结构,如何存储的? 如何加入元素?既然是hashMap,那么是如何计算hashcode的呢?遇到冲突又是如何解决的呢? 如何删除元素? 当容量不够时是 ...

  9. 各种操作中心Operation Center一览

    Operation Center在中国可能有很多种名称,例如指挥中心.运维室.总控中心等等,国外可能也有很多名称,不管名称如何,任何一个上规模得数据总心或者运维单位一般都有一个这样得中心,来负责所管理 ...

  10. 统计map上的read数量

    samtools flagstat /SRA111111/SRR111222/accepted_hits.bam 78406056 + 0 in total (QC-passed reads + QC ...