Springmvc线程池配置和使用

1.Springmvc线程池配置:

在spring上下文文件ApplicationContext.xml中添加

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

 

2.线程池的使用:

在需要调用的业务service调用线程

1)先定义引用

@Autowired
    private ThreadPoolTaskExecutor taskExecutor;

2)我这边操作的业务是给图片压缩 (这里的picList是图片集合,这里当参数传给线程类)

 

 private void uploadPic(){
	//图片集合
	List<String> picList=new ArrayList<String>();
	//上传图片业务逻辑
	...
	 //处理压缩图片任务
	 intLogger.info("DiseaseCourseInterface|uploadPic 当前线程数:"+taskExecutor.getActiveCount());
	 Schedule schedule=new Schedule(picList);
	 taskExecutor.execute(schedule);
 }

 3)创建一个线程类Schedule,处理压缩图片任务

public class Schedule implements Runnable{
    //日志
	protected Logger logger = Logger.getLogger(this.getClass());
	//参数处理
	private List<String> picList;
	

    public Schedule(List<String> picList) {
        this.picList = picList;
     }
    
    

    @Override
    public void run() {
    	logger.info("[Schedule][execute]method begin ...");
        try {
        	if(null!=picList&&picList.size()>0){
        		for (String pic : picList) {
        			//可直接获得图片list,这里处理压缩图片业务
				...
                }
    		}
         } catch (Exception e) {
        	 logger.error("[Schedule][execute]method error...");
        }
        logger.info("[Schedule][execute]method end ...");
    }
  }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值