ScheduledExecutorService定时周期执行指定的任务

来源:互联网 发布:dota1深渊领主技能数据 编辑:程序博客网 时间:2024/04/30 20:02

一:简单说明

ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便。

下面是该接口的原型定义

java.util.concurrent.ScheduleExecutorService extends ExecutorService extends Executor


接口scheduleAtFixedRate原型定义及参数说明

[java] view plaincopy
  1. public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,  
  2.             long initialDelay,  
  3.             long period,  
  4.             TimeUnit unit);  

command:执行线程
initialDelay:初始化延时
period:两次开始执行最小间隔时间
unit:计时单位

接口scheduleWithFixedDelay原型定义及参数说明
[java] view plaincopy
  1. public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,  
  2.                 long initialDelay,  
  3.                 long delay,  
  4.                 TimeUnit unit);  

command:执行线程
initialDelay:初始化延时
period:前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间)
unit:计时单位

二:功能示例

1.按指定频率周期执行某个任务。

初始化延迟0ms开始执行,每隔100ms重新执行一次任务。

[java] view plaincopy
  1. /** 
  2.  * 以固定周期频率执行任务 
  3.  */  
  4. public static void executeFixedRate() {  
  5.     ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);  
  6.     executor.scheduleAtFixedRate(  
  7.             new EchoServer(),  
  8.             0,  
  9.             100,  
  10.             TimeUnit.MILLISECONDS);  
  11. }  
间隔指的是连续两次任务开始执行的间隔。

2.按指定频率间隔执行某个任务。

初始化时延时0ms开始执行,本次执行结束后延迟100ms开始下次执行。

[java] view plaincopy
  1. /** 
  2.  * 以固定延迟时间进行执行 
  3.  * 本次任务执行完成后,需要延迟设定的延迟时间,才会执行新的任务 
  4.  */  
  5. public static void executeFixedDelay() {  
  6.     ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);  
  7.     executor.scheduleWithFixedDelay(  
  8.             new EchoServer(),  
  9.             0,  
  10.             100,  
  11.             TimeUnit.MILLISECONDS);  
  12. }  

3.周期定时执行某个任务。

有时候我们希望一个任务被安排在凌晨3点(访问较少时)周期性的执行一个比较耗费资源的任务,可以使用下面方法设定每天在固定时间执行一次任务。

[java] view plaincopy
  1. /** 
  2.  * 每天晚上8点执行一次 
  3.  * 每天定时安排任务进行执行 
  4.  */  
  5. public static void executeEightAtNightPerDay() {  
  6.     ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);  
  7.     long oneDay = 24 * 60 * 60 * 1000;  
  8.     long initDelay  = getTimeMillis("20:00:00") - System.currentTimeMillis();  
  9.     initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;  
  10.   
  11.     executor.scheduleAtFixedRate(  
  12.             new EchoServer(),  
  13.             initDelay,  
  14.             oneDay,  
  15.             TimeUnit.MILLISECONDS);  
  16. }  
[java] view plaincopy
  1. /** 
  2.  * 获取指定时间对应的毫秒数 
  3.  * @param time "HH:mm:ss" 
  4.  * @return 
  5.  */  
  6. private static long getTimeMillis(String time) {  
  7.     try {  
  8.         DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");  
  9.         DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");  
  10.         Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);  
  11.         return curDate.getTime();  
  12.     } catch (ParseException e) {  
  13.         e.printStackTrace();  
  14.     }  
  15.     return 0;  
  16. }  

4.辅助代码

[java] view plaincopy
  1. class EchoServer implements Runnable {  
  2.     @Override  
  3.     public void run() {  
  4.         try {  
  5.             Thread.sleep(50);  
  6.         } catch (InterruptedException e) {  
  7.             e.printStackTrace();  
  8.         }  
  9.         System.out.println("This is a echo server. The current time is " +  
  10.                 System.currentTimeMillis() + ".");  
  11.     }  
  12. }  

三:一些问题

上面写的内容有不严谨的地方,比如对于scheduleAtFixedRate方法,当我们要执行的任务大于我们指定的执行间隔时会怎么样呢?

对于中文API中的注释,我们可能会被忽悠,认为无论怎么样,它都会按照我们指定的间隔进行执行,其实当执行任务的时间大于我们指定的间隔时间时,它并不会在指定间隔时开辟一个新的线程并发执行这个任务。而是等待该线程执行完毕。

源码注释如下:

[java] view plaincopy
  1. * Creates and executes a periodic action that becomes enabled first  
  2. * after the given initial delay, and subsequently with the given  
  3. * period; that is executions will commence after  
  4. * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then  
  5. * <tt>initialDelay + 2 * period</tt>, and so on.  
  6. * If any execution of the task  
  7. * encounters an exception, subsequent executions are suppressed.  
  8. * Otherwise, the task will only terminate via cancellation or  
  9. * termination of the executor.  If any execution of this task  
  10. * takes longer than its period, then subsequent executions  
  11. * may start late, but will not concurrently execute.  

根据注释中的内容,我们需要注意的时,我们需要捕获最上层的异常,防止出现异常中止执行,导致周期性的任务不再执行。

四:除了我们自己实现定时任务之外,我们可以使用Spring帮我们完成这样的事情。

Spring自动定时任务配置方法(我们要执行任务的类名为com.study.MyTimedTask)

[html] view plaincopy
  1. <bean id="myTimedTask" class="com.study.MyTimedTask"/>  

[html] view plaincopy
  1. <bean id="doMyTimedTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  2.     <property name="targetObject" ref="myTimedTask"/>  
  3.     <property name="targetMethod" value="execute"/>  
  4.     <property name="concurrent" value="false"/>  
  5. </bean>  

[html] view plaincopy
  1. <bean id="myTimedTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  2.     <property name="jobDetail" ref="doMyTimedTask"/>  
  3.     <property name="cronExpression" value="0 0 2 * ?"/>  
  4. </bean>  

[html] view plaincopy
  1. <bean id="doScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  2.     <property name="triggers">  
  3.         <list>  
  4.             <ref local="myTimedTaskTrigger"/>  
  5.         </list>  
  6.     </property>  
  7. </bean>  

[html] view plaincopy
  1. <bean id="doScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  2.     <property name="triggers">  
  3.         <list>  
  4.             <bean class="org.springframework.scheduling.quartz.CronTriggerBean">  
  5.                 <property name="jobDetail"/>  
  6.                     <bean id="doMyTimedTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  7.                         <property name="targetObject">  
  8.                             <bean class="com.study.MyTimedTask"/>  
  9.                         </property>  
  10.                         <property name="targetMethod" value="execute"/>  
  11.                         <property name="concurrent" value="false"/>  
  12.                     </bean>  
  13.                 </property>  
  14.                 <property name="cronExpression" value="0 0 2 * ?"/>  
  15.             </bean>  
  16.         </list>  
  17.     </property>  
  18. </bean> 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 5天新生儿不拉屎怎么办 4月宝宝不拉屎怎么办 两岁宝宝晚上睡觉哭闹怎么办 2月婴儿吐奶很多怎么办 心情不好回奶了怎么办 四个月了没奶怎么办 八岁宝宝还尿床怎么办 自己一人在家害怕怎么办 被猫抓伤流血了怎么办 被小狐狸咬了怎么办 怀孕吃了兔子肉怎么办 鸟总在窗子上啄怎么办? 有鸟飞进楼道里怎么办 租的房间房东要求改建怎么办 小偷偷钱抓住不还钱怎么办 损友圈羊被陌生人偷了怎么办 在酒店如果遇到客人偷东西怎么办 梦见家里有不好的东西怎么办 被小孩要破了怎么办啊 租一个房子小孩一进房就哭怎么办 墙缝里有蝙蝠窝怎么办 小蝙蝠在墙缝里怎么办 小孩一进屋就哭怎么办 屋门对着厕所门怎么办 入室门对厨房门怎么办 厕所正对入户门怎么办 小区楼交错冲路怎么办 床的位置在五鬼上怎么办 被甩了很痛苦怎么办 和对象想分手了怎么办 对象想跟你啪啪怎么办 相亲对象好像不太想理我怎么办 想跟对象分手了怎么办 异地恋分手后该怎么办 面膜敷了一晚上怎么办 梦见被刺猬咬了怎么办 梦见死人叫我名字答应怎么办 香瓜苗叶子长斑怎么办 奶油打出来很稀怎么办 寄的水果压坏了怎么办 吃了一个烂水果怎么办