定时任务的几种实现

  • 一.分类

从实现的技术上来分类,目前主要有三种技术(或者说有三种产品):

  1.  Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少,这篇文章将不做详细介绍。
  2. 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,稍后会详细介绍。
  3. Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,稍后会介绍。

从作业类的继承方式来讲,可以分为两类:

  1. 作业类需要继承自特定的作业类基类,如Quartz中需要继承自org.springframework.scheduling.quartz.QuartzJobBean;java.util.Timer中需要继承自java.util.TimerTask。
  2. 作业类即普通的java类,不需要继承自任何基类。

注:个人推荐使用第二种方式,因为这样所以的类都是普通类,不需要事先区别对待。

从任务调度的触发时机来分,这里主要是针对作业使用的触发器,主要有以下两种:

  1. 每隔指定时间则触发一次,在Quartz中对应的触发器为:org.springframework.scheduling.quartz.SimpleTriggerBean
  2. 每到指定时间则触发一次,在Quartz中对应的调度器为:org.springframework.scheduling.quartz.CronTriggerBean

注:并非每种任务都可以使用这两种触发器,如java.util.TimerTask任务就只能使用第一种。Quartz和spring task都可以支持这两种触发条件。

  • 二.用法说明

  • Quartz

  1. 第一种,作业类需要继承自特定的作业类基类

作业类继承自特定的基类:org.springframework.scheduling.quartz.QuartzJobBean。

        第一步:定义作业类
  1. import org.quartz.JobExecutionContext;  
    import org.quartz.JobExecutionException;  
    import org.springframework.scheduling.quartz.QuartzJobBean;  
    public class Job1 extends QuartzJobBean {  
      
    private int timeout;  
    private static int i = 0;  
    //调度工厂实例化后,经过timeout时间开始执行调度  
    public void setTimeout(int timeout) {  
    this.timeout = timeout;  
    }  
      
    /** 
    * 要调度的具体任务 
    */  
    @Override  
    protected void executeInternal(JobExecutionContext context)  
    throws JobExecutionException {  
      System.out.println("定时任务执行中…");  
    }  
    }  
    第二步:spring配置文件中配置作业类JobDetailBean
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">  
<property name="jobClass" value="com.gy.Job1" />  
<property name="jobDataAsMap">  
<map>  
<entry key="timeout" value="0" />  
</map>  
</property>  
</bean>  

说明:org.springframework.scheduling.quartz.JobDetailBean有两个属性,jobClass属性即我们在java代码中定义的任务类,jobDataAsMap属性即该任务类中需要注入的属性值。

第三步:配置作业调度的触发方式(触发器)

Quartz的作业触发器有两种,分别是

org.springframework.scheduling.quartz.SimpleTriggerBean

org.springframework.scheduling.quartz.CronTriggerBean

 

第一种SimpleTriggerBean,只支持按照一定频度调用任务,如每隔30分钟运行一次。

配置方式如下:

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
<property name="jobDetail" ref="job1" />  
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->  
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->  
</bean>  

第二种CronTriggerBean,支持到指定时间运行一次,如每天12:00运行一次等。

配置方式如下:

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
<property name="jobDetail" ref="job1" />  
<!—每天12:00运行一次 -->  
<property name="cronExpression" value="0 0 12 * * ?" />  
</bean>  
第四步:配置调度工厂 
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
<property name="triggers">  
<list>  
<ref bean="cronTrigger" />  
</list>  
</property>  
</bean>  

 说明:该参数指定的就是之前配置的触发器的名字。

第五步:启动你的应用即可,即将工程部署至tomcat或其他容器。

第二种,作业类不继承特定基类。

Spring能够支持这种方式,归功于两个类:

org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean

org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

这两个类分别对应spring支持的两种实现任务调度的方式,即前文提到到java自带的timer task方式和Quartz方式。这里我只写MethodInvokingJobDetailFactoryBean的用法,使用该类的好处是,我们的任务类不再需要继承自任何类,而是普通的pojo。

第一步:编写任务类
public class Job2 {  
public void doJob2() {  
System.out.println("不继承QuartzJobBean方式-调度进行中...");  
}  
}  

 可以看出,这就是一个普通的类,并且有一个方法。

第二步:配置作业类
<bean id="job2"  
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
<property name="targetObject">  
<bean class="com.gy.Job2" />  
</property>  
<property name="targetMethod" value="doJob2" />  
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->  
</bean>  

 说明:这一步是关键步骤,声明一个MethodInvokingJobDetailFactoryBean,有两个关键属性:targetObject  第二种CronTriggerBean,支持到指定时间运行一次,如每天12:00运行一次等。

配置方式如下:

指定任务类,targetMethod指定运行的方法。往下的步骤就与方法一相同了,为了完整,同样贴出。

第三步:配置作业调度的触发方式(触发器)

Quartz的作业触发器有两种,分别是

org.springframework.scheduling.quartz.SimpleTriggerBean

org.springframework.scheduling.quartz.CronTriggerBean

第一种SimpleTriggerBean只支持按照一定频度调用任务,如每隔30分钟运行一次。

配置方式如下:

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
<property name="jobDetail" ref="job2" />  
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->  
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->  
</bean>  

 

 第二种CronTriggerBean,支持到指定时间运行一次,如每天12:00运行一次等。

配置方式如下:

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
<property name="jobDetail" ref="job2" />  
<!—每天12:00运行一次 -->  
<property name="cronExpression" value="0 0 12 * * ?" />  
</bean>  

以上两种调度方式根据实际情况,任选一种即可。

第四步:配置调度工厂 
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
<property name="triggers">  
<list>  
<ref bean="cronTrigger" />  
</list>  
</property>  
</bean>  

说明:该参数指定的就是之前配置的触发器的名字。

第五步:启动你的应用即可,即将工程部署至tomcat或其他容器。

 

到此,spring中Quartz的基本配置就介绍完了,当然了,使用之前,要导入相应的spring的包与Quartz的包,这些就不消多说了。

其实可以看出Quartz的配置看上去还是挺复杂的,没有办法,因为Quartz其实是个重量级的工具,如果我们只是想简单的执行几个简单的定时任务,有没有更简单的工具,有!

 

Spring-Task

pring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式,下面将分别介绍这两种方式。

第一种:配置文件方式

第一步:编写作业类

即普通的pojo,如下:

import org.springframework.stereotype.Service;  
@Service  
public class TaskJob {  
      
    public void job1() {  
        System.out.println(“任务进行中。。。”);  
    }  
}  

  

第二步:在spring配置文件头中添加命名空间及描述

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:task="http://www.springframework.org/schema/task"   
    。。。。。。  
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  

 第三步:spring配置文件中设置具体的任务

 <task:scheduled-tasks>   
        <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   
</task:scheduled-tasks>  
  
<context:component-scan base-package=" com.gy.mytask " />  

说明:ref参数指定的即任务类,method指定的即需要运行的方法,cron即cronExpression表达式,具体写法这里不介绍了,详情见上篇文章附录。

<context:component-scan base-package="com.gy.mytask" />这个配置不消多说了,spring扫描注解用的。

到这里配置就完成了,是不是很简单。

第二种:使用注解形式

也许我们不想每写一个任务类还要在xml文件中配置下,我们可以使用注解@Scheduled,我们看看源文件中该注解的定义:

@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface Scheduled  
{  
  public abstract String cron();  
  
  public abstract long fixedDelay();  
  
  public abstract long fixedRate();  
}  

 可以看出该注解有三个方法或者叫参数,分别表示的意思是:

cron:指定cron表达式

fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。

fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

 

下面我来配置一下。

第一步:编写pojo
import org.springframework.scheduling.annotation.Scheduled;    
import org.springframework.stereotype.Component;  
  
@Component(“taskJob”)  
public class TaskJob {  
    @Scheduled(cron = "0 0 3 * * ?")  
    public void job1() {  
        System.out.println(“任务进行中。。。”);  
    }  
}  
第二步:添加task相关的配置:
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:task="http://www.springframework.org/schema/task"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
        http://www.springframework.org/schema/context   
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"  
    default-lazy-init="false">  
  
  
    <context:annotation-config />  
    <!—spring扫描注解的配置   -->  
    <context:component-scan base-package="com.gy.mytask" />  
      
<!—开启这个配置,spring才能识别@Scheduled注解   -->  
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/>  

说明:理论上只需要加上<task:annotation-driven />这句配置就可以了,这些参数都不是必须的。

 

Java Timer定时器

   在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现。下面就其原理、实例以及Timer缺陷三个方面来解析Java Timer定时器。

 

一、简介

      在Java中一个完整定时任务需要由Timer、TimerTask两个类来配合完成。 API中是这样定义他们的,Timer:一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。由TimerTask:Timer 安排为一次执行或重复执行的任务。我们可以这样理解Timer是一种定时器工具,用来在一个后台线程计划执行指定任务,而TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务。

Timer

      在工具类Timer中,提供了四个构造方法,每个构造方法都启动了计时器线程,同时Timer类可以保证多个线程可以共享单个Timer对象而无需进行外部同步,所以Timer类是线程安全的。但是由于每一个Timer对象对应的是单个后台线程,用于顺序执行所有的计时器任务,一般情况下我们的线程任务执行所消耗的时间应该非常短,但是由于特殊情况导致某个定时器任务执行的时间太长,那么他就会“独占”计时器的任务执行线程,其后的所有线程都必须等待它执行完,这就会延迟后续任务的执行,使这些任务堆积在一起,具体情况我们后面分析。

      当程序初始化完成Timer后,定时任务就会按照我们设定的时间去执行,Timer提供了schedule方法,该方法有多中重载方式来适应不同的情况,如下:

      schedule(TimerTask task, Date time):安排在指定的时间执行指定的任务。

      schedule(TimerTask task, Date firstTime, long period) :安排指定的任务在指定的时间开始进行重复的固定延迟执行。

      schedule(TimerTask task, long delay) :安排在指定延迟后执行指定的任务。

      schedule(TimerTask task, long delay, long period) :安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。

      同时也重载了scheduleAtFixedRate方法,scheduleAtFixedRate方法与schedule相同,只不过他们的侧重点不同,区别后面分析。

      scheduleAtFixedRate(TimerTask task, Date firstTime, long period):安排指定的任务在指定的时间开始进行重复的固定速率执行。

      scheduleAtFixedRate(TimerTask task, long delay, long period):安排指定的任务在指定的延迟后开始进行重复的固定速率执行。

TimerTask

      TimerTask类是一个抽象类,由Timer 安排为一次执行或重复执行的任务。它有一个抽象方法run()方法,该方法用于执行相应计时器任务要执行的操作。因此每一个具体的任务类都必须继承TimerTask,然后重写run()方法。

      另外它还有两个非抽象的方法:

      boolean cancel():取消此计时器任务。

      long scheduledExecutionTime():返回此任务最近实际执行的安排执行时间。

 

二、实例

1.指定延迟时间执行定时任务
public class TimerTest01 {  
    Timer timer;  
    public TimerTest01(int time){  
        timer = new Timer();  
        timer.schedule(new TimerTaskTest01(), time * 1000);  
    }  
      
    public static void main(String[] args) {  
        System.out.println("timer begin....");  
        new TimerTest01(3);  
    }  
}  
  
public class TimerTaskTest01 extends TimerTask{  
  
    public void run() {  
        System.out.println("Time's up!!!!");  
    }  
}  

 

  运行结果:

首先打印:timer begin....  
  
3秒后打印:Time's up!!!!  
2.在指定时间执行定时任务
public class TimerTest02 {  
    Timer timer;  
      
    public TimerTest02(){  
        Date time = getTime();  
        System.out.println("指定时间time=" + time);  
        timer = new Timer();  
        timer.schedule(new TimerTaskTest02(), time);  
    }  
      
    public Date getTime(){  
        Calendar calendar = Calendar.getInstance();  
        calendar.set(Calendar.HOUR_OF_DAY, 11);  
        calendar.set(Calendar.MINUTE, 39);  
        calendar.set(Calendar.SECOND, 00);  
        Date time = calendar.getTime();  
          
        return time;  
    }  
      
    public static void main(String[] args) {  
        new TimerTest02();  
    }  
}  
  
public class TimerTaskTest02 extends TimerTask{  
  
    @Override  
    public void run() {  
        System.out.println("指定时间执行线程任务...");  
    }  
}  

 当时间到达11:39:00时就会执行该线程任务,当然大于该时间也会执行!!执行结果为:

指定时间time=Tue Jun 10 11:39:00 CST 2014  
指定时间执行线程任务...
3.在延迟指定时间后以指定的间隔时间循环执行定时任务
public class TimerTest03 {  
    Timer timer;  
      
    public TimerTest03(){  
        timer = new Timer();  
        timer.schedule(new TimerTaskTest03(), 1000, 2000);  
    }  
      
    public static void main(String[] args) {  
        new TimerTest03();  
    }  
}  
  
public class TimerTaskTest03 extends TimerTask{  
  
    @Override  
    public void run() {  
        Date date = new Date(this.scheduledExecutionTime());  
        System.out.println("本次执行该线程的时间为:" + date);  
    }  
}  

运行结果:

本次执行该线程的时间为:Tue Jun 10 21:19:47 CST 2014  
本次执行该线程的时间为:Tue Jun 10 21:19:49 CST 2014  
本次执行该线程的时间为:Tue Jun 10 21:19:51 CST 2014  
本次执行该线程的时间为:Tue Jun 10 21:19:53 CST 2014  
本次执行该线程的时间为:Tue Jun 10 21:19:55 CST 2014  
本次执行该线程的时间为:Tue Jun 10 21:19:57 CST 2014  
.................  

   对于这个线程任务,如果我们不将该任务停止,他会一直运行下去。

      scheduleAtFixedRate方法与schedule方法一样!

4.分析schedule和scheduleAtFixedRate

      1schedule(TimerTask task, Date time)schedule(TimerTask task, long delay)

      对于这两个方法而言,如果指定的计划执行时间scheduledExecutionTime<= systemCurrentTime,则task会被立即执行。scheduledExecutionTime不会因为某一个task的过度执行而改变。

      2schedule(TimerTask task, Date firstTime, long period)schedule(TimerTask task, long delay, long period)

      这两个方法与上面两个就有点儿不同的,前面提过Timer的计时器任务会因为前一个任务执行时间较长而延时。在这两个方法中,每一次执行的task的计划时间会随着前一个task的实际时间而发生改变,也就是scheduledExecutionTime(n+1)=realExecutionTime(n)+periodTime。也就是说如果第n个task由于某种情况导致这次的执行时间过程,最后导致systemCurrentTime>= scheduledExecutionTime(n+1),这是第n+1个task并不会因为到时了而执行,他会等待第n个task执行完之后再执行,那么这样势必会导致n+2个的执行实现scheduledExecutionTime放生改变即scheduledExecutionTime(n+2) = realExecutionTime(n+1)+periodTime。所以这两个方法更加注重保存间隔时间的稳定。

3.scheduleAtFixedRate(TimerTask task, Date firstTime, long period)scheduleAtFixedRate(TimerTask task, long delay, long period)

      在前面也提过scheduleAtFixedRate与schedule方法的侧重点不同,schedule方法侧重保存间隔时间的稳定,而scheduleAtFixedRate方法更加侧重于保持执行频率的稳定。为什么这么说,原因如下。在schedule方法中会因为前一个任务的延迟而导致其后面的定时任务延时,而scheduleAtFixedRate方法则不会,如果第n个task执行时间过长导致systemCurrentTime>= scheduledExecutionTime(n+1),则不会做任何等待他会立即执行第n+1个task,所以scheduleAtFixedRate方法执行时间的计算方法不同于schedule,而是scheduledExecutionTime(n)=firstExecuteTime +n*periodTime,该计算方法永远保持不变。所以scheduleAtFixedRate更加侧重于保持执行频率的稳定。

三、Timer的缺陷

3.1、Timer的缺陷

      Timer计时器可以定时(指定时间执行任务)、延迟(延迟5秒执行任务)、周期性地执行任务(每隔个1秒执行任务),但是,Timer存在一些缺陷。首先Timer对调度的支持是基于绝对时间的,而不是相对时间,所以它对系统时间的改变非常敏感。其次Timer线程是不会捕获异常的,如果TimerTask抛出的了未检查异常则会导致Timer线程终止,同时Timer也不会重新恢复线程的执行,他会错误的认为整个Timer线程都会取消。同时,已经被安排单尚未执行的TimerTask也不会再执行了,新的任务也不能被调度。故如果TimerTask抛出未检查的异常,Timer将会产生无法预料的行为。

 1Timer管理时间延迟缺陷

      前面Timer在执行定时任务时只会创建一个线程任务,如果存在多个线程,若其中某个线程因为某种原因而导致线程任务执行时间过长,超过了两个任务的间隔时间,会发生一些缺陷:

public class TimerTest04 {  
    private Timer timer;  
    public long start;     
      
    public TimerTest04(){  
        this.timer = new Timer();  
        start = System.currentTimeMillis();  
    }  
      
    public void timerOne(){  
        timer.schedule(new TimerTask() {  
            public void run() {  
                System.out.println("timerOne invoked ,the time:" + (System.currentTimeMillis() - start));  
                try {  
                    Thread.sleep(4000);    //线程休眠3000  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }, 1000);  
    }  
      
    public void timerTwo(){  
        timer.schedule(new TimerTask() {  
            public void run() {  
                System.out.println("timerOne invoked ,the time:" + (System.currentTimeMillis() - start));  
            }  
        }, 3000);  
    }  
      
    public static void main(String[] args) throws Exception {  
        TimerTest04 test = new TimerTest04();  
          
        test.timerOne();  
        test.timerTwo();  
    }  
}  

 

按照我们正常思路,timerTwo应该是在3s后执行,其结果应该是:

timerOne invoked ,the time:1001  
timerOne invoked ,the time:3001  

  但是事与愿违,timerOne由于sleep(4000),休眠了4S,同时Timer内部是一个线程,导致timeOne所需的时间超过了间隔时间,结果:

timerOne invoked ,the time:1000  
timerOne invoked ,the time:5000  

2Timer抛出异常缺陷

 如果TimerTask抛出RuntimeException,Timer会终止所有任务的运行。如下:

public class TimerTest04 {  
    private Timer timer;  
      
    public TimerTest04(){  
        this.timer = new Timer();  
    }  
      
    public void timerOne(){  
        timer.schedule(new TimerTask() {  
            public void run() {  
                throw new RuntimeException();  
            }  
        }, 1000);  
    }  
      
    public void timerTwo(){  
        timer.schedule(new TimerTask() {  
              
            public void run() {  
                System.out.println("我会不会执行呢??");  
            }  
        }, 1000);  
    }  
      
    public static void main(String[] args) {  
        TimerTest04 test = new TimerTest04();  
        test.timerOne();  
        test.timerTwo();  
    }  
}  

    运行结果:timerOne抛出异常,导致timerTwo任务终止。

Exception in thread "Timer-0" java.lang.RuntimeException  
    at com.chenssy.timer.TimerTest04$1.run(TimerTest04.java:25)  
    at java.util.TimerThread.mainLoop(Timer.java:555)  
    at java.util.TimerThread.run(Timer.java:505)  

      对于Timer的缺陷,我们可以考虑 ScheduledThreadPoolExecutor 来替代。Timer是基于绝对时间的,对系统时间比较敏感,而ScheduledThreadPoolExecutor 则是基于相对时间;Timer是内部是单一线程,而ScheduledThreadPoolExecutor内部是个线程池,所以可以支持多个任务并发执行。

3.2、用ScheduledExecutorService替代Timer

1、解决问题一:

public class ScheduledExecutorTest {  
    private  ScheduledExecutorService scheduExec;  
      
    public long start;  
      
    ScheduledExecutorTest(){  
        this.scheduExec =  Executors.newScheduledThreadPool(2);    
        this.start = System.currentTimeMillis();  
    }  
      
    public void timerOne(){  
        scheduExec.schedule(new Runnable() {  
            public void run() {  
                System.out.println("timerOne,the time:" + (System.currentTimeMillis() - start));  
                try {  
                    Thread.sleep(4000);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        },1000,TimeUnit.MILLISECONDS);  
    }  
      
    public void timerTwo(){  
        scheduExec.schedule(new Runnable() {  
            public void run() {  
                System.out.println("timerTwo,the time:" + (System.currentTimeMillis() - start));  
            }  
        },2000,TimeUnit.MILLISECONDS);  
    }  
      
    public static void main(String[] args) {  
        ScheduledExecutorTest test = new ScheduledExecutorTest();  
        test.timerOne();  
        test.timerTwo();  
    }  
}  

 

运行结果:

timerOne,the time:1003  
timerTwo,the time:2005  

  2、解决问题二

public class ScheduledExecutorTest {  
    private  ScheduledExecutorService scheduExec;  
      
    public long start;  
      
    ScheduledExecutorTest(){  
        this.scheduExec =  Executors.newScheduledThreadPool(2);    
        this.start = System.currentTimeMillis();  
    }  
      
    public void timerOne(){  
        scheduExec.schedule(new Runnable() {  
            public void run() {  
                throw new RuntimeException();  
            }  
        },1000,TimeUnit.MILLISECONDS);  
    }  
      
    public void timerTwo(){  
        scheduExec.scheduleAtFixedRate(new Runnable() {  
            public void run() {  
                System.out.println("timerTwo invoked .....");  
            }  
        },2000,500,TimeUnit.MILLISECONDS);  
    }  
      
    public static void main(String[] args) {  
        ScheduledExecutorTest test = new ScheduledExecutorTest();  
        test.timerOne();  
        test.timerTwo();  
    }  
}  

运行结果:

timerTwo invoked .....  
timerTwo invoked .....  
timerTwo invoked .....  
timerTwo invoked .....  
timerTwo invoked .....  
timerTwo invoked .....  
timerTwo invoked .....  
timerTwo invoked .....  
timerTwo invoked .....  
........................  

 

三、附录:cronExpression表达式

格式: [秒] [分] [小时] [日] [月] [周] [年]

 序号

说明 

 是否必填

 允许填写的值

允许的通配符 

 1

 秒

 是

 0-59 

  , - * /

 2

 分

 是

 0-59 

  , - * /

 3

小时

 是

 0-23

  , - * /

 4

 日

 是

 1-31

  , - * ? / L W

 5

 月

 是

 1-12 or JAN-DEC

  , - * /

 6

 周

 是

 1-7 or SUN-SAT

  , - * ? / L #

 7

 年

 否

 empty 或 1970-2099

 , - * /

通配符说明:

* 表示所有值. 例如:在分的字段上设置 "*",表示每一分钟都会触发。
? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10 * ?
- 表示区间。例如 在小时上设置 "10-12",表示 10,11,12点都会触发。
, 表示指定多个值,例如在周字段上设置 "MON,WED,FRI" 表示周一,周三和周五触发
/ 用于递增触发。如在秒上面设置"5/15" 表示从5秒开始,每增15秒触发(5,20,35,50)。 在月字段上设置'1/3'所示每月1号开始,每隔三天触发一次。
L 表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于"7"或"SAT"。如果在"L"前加上数字,则表示该数据的最后一个。例如在周字段上设置"6L"这样的格式,则表示“本 月最后一个星期五" 
W 表示离指定日期的最近那个工作日(周一至周五). 例如在日字段上设置"15W",表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果指定格式为 "1W",它则表示每月1号往后最近的工作日触发。如果1号正是周六,则将在3号下周一触发。(注,"W"前只能设置具体的数字,不允许区间"-").

小提示

'L'和 'W'可以一组合使用。如果在日字段上设置"LW",则表示在本月的最后一个工作日触发(一般指发工资 ) 

# 序号(表示每月的第几个周几),例如在周字段上设置"6#3"表示在每月的第三个周六.注意如果指定"#5",正好第五周没有周六,则不会触发该配置(用在母亲节和父亲节再合适不过了)

  

小提示

周字段的设置,若使用英文字母是不区分大小写的 MON 与mon相同.

常用示例:

0 0 12 * * ?

每天12点触发

0 15 10 ? * *

每天10点15分触发

0 15 10 * * ?

每天10点15分触发

0 15 10 * * ? *

每天10点15分触发

0 15 10 * * ? 2005

2005年每天10点15分触发

0 * 14 * * ?

每天下午的 2点到2点59分每分触发

0 0/5 14 * * ?

每天下午的 2点到2点59分(整点开始,每隔5分触发)

0 0/5 14,18 * * ?

每天下午的 2点到2点59分(整点开始,每隔5分触发)
每天下午的 18点到18点59分(整点开始,每隔5分触发)

0 0-5 14 * * ?

每天下午的 2点到2点05分每分触发

0 10,44 14 ? 3 WED

3月分每周三下午的 2点10分和2点44分触发

0 15 10 ? * MON-FRI

从周一到周五每天上午的10点15分触发

0 15 10 15 * ?

每月15号上午10点15分触发

0 15 10 L * ?

每月最后一天的10点15分触发

0 15 10 ? * 6L

每月最后一周的星期五的10点15分触发

0 15 10 ? * 6L 2002-2005

从2002年到2005年每月最后一周的星期五的10点15分触发

0 15 10 ? * 6#3

每月的第三周的星期五开始触发

0 0 12 1/5 * ?

每月的第一个中午开始每隔5天触发一次

0 11 11 11 11 ?

每年的11月11号 11点11分触发(光棍节)

 

转载于:https://my.oschina.net/u/3479706/blog/900710

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值