spring定时任务+线池程实现

之前看了程序员杂志上关于spring 定时任务实现异步任务的文章,自己架了一套帮助实现一些费时的操作。

在实现中发现几个问题

 

1、定时任务中时间设置是这样的

Java代码   收藏代码
  1. <property name="delay" value="1000" />  
  2. <!-- 每次任务间隔 5秒-->  
  3. <property name="period" value="5000" />  

 

在某些配置下某任务开始后还没执行完过了5秒,第二个任务又起来了。

这与我的设计冲突。我希望任务是执行完后等5秒再进行第二个任务。

最后发现这个类可以

Java代码   收藏代码
  1. <bean id="springScheduleExecutorTask"  
  2.     class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">  
  3.         <!-- 配置主任务 -->  
  4.         <property name="runnable" ref="mainExecutor" />  
  5.         <!-- 程序启动后延迟1秒执行第一次任务 -->  
  6.         <property name="delay" value="1000" />  
  7.         <!-- 每次任务间隔 5秒-->  
  8.         <property name="period" value="5000" />  
  9. </bean>  

 

2、在主任务中我把小任务分放给线程池操作,必须要等线程池完成工作后才能结束主任务run

    如果起动线程池主任务run完毕,第二个任务5秒之后就运行了。我实现的代码是

Java代码   收藏代码
  1. //等待线程执行完毕  
  2.         while(threadPool.getActiveCount() > 0){  
  3.             try{  
  4.                 Thread.sleep(1000);  
  5.             }catch(Exception e){  
  6.                 e.printStackTrace();  
  7.             }  
  8.         }  

   其实也可以采用观察者模式让线程池中任务发消息给主任务,主任务等待,

这个我之前有发给文章。 http://guoba6688-sina-com.iteye.com/blog/719972

 

附上所有代码

 

 

Java代码   收藏代码
  1. package com.my.task;  
  2.   
  3. import java.util.TimerTask;  
  4.   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;  
  7.   
  8. /** 
  9.  *  
  10.  * 主任务起动 
  11.  * 
  12.  * @author 锅巴 
  13.  * @version 1.0 2010-7-29 
  14.  */  
  15. public class MainExecutor extends TimerTask {  
  16.   
  17.     private ThreadPoolTaskExecutor threadPool;  
  18.       
  19.     @Override  
  20.     public void run() {  
  21.         // TODO Auto-generated method stub  
  22.         System.out.println("MainExecutor is start");  
  23.         try{  
  24.             Thread.sleep(5000);  
  25.         }catch(Exception e){  
  26.             e.printStackTrace();  
  27.         }  
  28.         threadPool.execute(new MyTask(10));  
  29.         threadPool.execute(new MyTask(10));  
  30.         threadPool.execute(new MyTask(10));  
  31.           
  32.         //等待线程执行完毕  
  33.         while(threadPool.getActiveCount() > 0){  
  34.             try{  
  35.                 Thread.sleep(1000);  
  36.             }catch(Exception e){  
  37.                 e.printStackTrace();  
  38.             }  
  39.         }  
  40.           
  41.         System.out.println("MainExecutor is end");  
  42.           
  43.     }  
  44.   
  45.     public ThreadPoolTaskExecutor getThreadPool() {  
  46.         return threadPool;  
  47.     }  
  48.   
  49.     public void setThreadPool(ThreadPoolTaskExecutor threadPool) {  
  50.         this.threadPool = threadPool;  
  51.     }  
  52.   
  53.     public static void main(String[] args) {  
  54.         new ClassPathXmlApplicationContext(new String[]{"task.context.xml"});  
  55.     }  
  56.       
  57. }  

 

 

 

Java代码   收藏代码
  1. package com.my.task;  
  2.   
  3. public class MyTask implements Runnable{  
  4.   
  5.     private int count;  
  6.       
  7.     public MyTask(int count){  
  8.         this.count = count;  
  9.     }  
  10.       
  11.     @Override  
  12.     public void run() {  
  13.         // TODO Auto-generated method stub  
  14.       
  15.         for(int i=0; i<count; i++){  
  16.             System.out.println(Thread.currentThread().getName() + " : " + i);   
  17.         }  
  18.         System.out.println(Thread.currentThread().getName() + " end ");  
  19.           
  20.     }  
  21.   
  22. }  

 

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/spring-beans.dtd">  
  3. <beans default-autowire="byName">  
  4.   
  5.   
  6.      
  7.     <bean id="springScheduledExecutorFactoryBean"  
  8.         class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">  
  9.         <property name="scheduledExecutorTasks">  
  10.             <list>  
  11.                 <ref bean="springScheduleExecutorTask" />  
  12.             </list>  
  13.         </property>  
  14.     </bean>  
  15.   
  16.       
  17.     <bean id="springScheduleExecutorTask"  
  18.         class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">  
  19.           
  20.         <property name="runnable" ref="mainExecutor" />  
  21.           
  22.         <property name="delay" value="1000" />  
  23.         <!-- 每次任务间隔 5秒-->  
  24.         <property name="period" value="5000" />  
  25.     </bean>  
  26.   
  27.     <!-- 主任务 负责扫描任务 将任务分配给线程完成 -->  
  28.     <bean id="mainExecutor"  
  29.         class="com.my.task.MainExecutor">  
  30.         <property name="threadPool" ref="threadPool" />  
  31.     </bean>  
  32.   
  33.     <!-- 异步线程池 -->  
  34.     <bean id="threadPool"  
  35.         class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
  36.         <!-- 核心线程数  -->  
  37.         <property name="corePoolSize" value="10" />  
  38.         <!-- 最大线程数 -->  
  39.         <property name="maxPoolSize" value="50" />  
  40.         <!-- 队列最大长度 >=mainExecutor.maxSize -->  
  41.         <property name="queueCapacity" value="1000" />  
  42.         <!-- 线程池维护线程所允许的空闲时间 -->  
  43.         <property name="keepAliveSeconds" value="300" />  
  44.         <!-- 线程池对拒绝任务(无线程可用)的处理策略 -->  
  45.         <property name="rejectedExecutionHandler">  
  46.             <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/>  
  47.         </property>  
  48.     </bean>  
  49.   
  50. </beans>  
 
this document is coming from http://www.360doc.com/content/12/0702/15/1332348_221723975.shtml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值