02_张孝祥_Java多线程_传统定时器Timer

来源:互联网 发布:js中eval函数的用法 编辑:程序博客网 时间:2024/05/21 06:41

Timer的缺陷

Timer的替代品ScheduledExecutorService简介

创建

到点执行,参数(TimerTask task, Date time),或者(TimerTask task, long delay)延迟多久后执行

new Timer().schedule(new TimerTask() {    @Override    public void run() {    }}, new Date());

延迟多久执行,然后定时执行,参数(TimerTask task, long delay, long period)或者(TimerTask task, Date firstTime, long period)到点执行,然后定时执行

new Timer().schedule(new TimerTask() {    @Override    public void run() {    }

还有类似的scheduleAtFixedRate(TimerTask task, long delay, long period)scheduleAtFixedRate(TimerTask task, Date firstTime,long period)

schedule和scheduleAtFixedRate区别

  • 2个参数的schedule:如果当前时间超过第一次执行时间,则立即执行,否则到定时时间执行
  • 3个参数的schedule:如果当前时间超过第一次执行时间,则立即执行,否则到定时时间执行。下一个任务执行一定是在上一个任务执行完之后执行。下一次任务执行的时间需要看上一个任务执行多久exceTime及周期时间period,如果exceTime>period则立即执行,否则等待period时间再执行。
  • scheduleAtFixedRate:每个任务执行的时间应该是定下了的。如果中间有作业处理时间太长导致后面的不能如期定时执行,则会立即执行后面的作业,直到追上了某一个任务的定时。如果当前时间超过第一次执行时间,则后面所有的作业都会立即执行,直到追上了某一个任务的定时。因为fixed-rate,可能导致同一时间重复执行,所以TimerTask中的执行体需要考虑同步(不是很懂)

schedule示例

new Timer().schedule(new TimerTask(){    public void run() {          try {            System.out.println("execute task!  "+ dateFormatter.format(this.scheduledExecutionTime()));            Random random = new Random();            int slpTime = random.nextInt(3)*1000 + 4000;            System.out.println("exec time:" + slpTime);            Thread.sleep(slpTime);        } catch (InterruptedException e) {            e.printStackTrace();           }      }},startDate, 5 * 1000);

输出结果

execute task!  2017-12-14 23:26:22 // 当前时间超过了设定了首次执行时间,立即执行exec time:1000                     // 第一次执行时间为1s小于周期时间2sexecute task!  2017-12-14 23:26:24 // 所以第二次在第一次执行时间2s之后执行exec time:2000                     // 第三次次执行时间为2s刚好等于周期时间2sexecute task!  2017-12-14 23:26:26 // 所以第三次在第二次执行时间2s之后执行exec time:3000                     // 第三次执行时间为3s大于周期时间2sexecute task!  2017-12-14 23:26:29 // 所以第四次在第三次执行时间3s之后执行exec time:1000                     // 之后就类似execute task!  2017-12-14 23:26:31exec time:2000execute task!  2017-12-14 23:26:33exec time:3000execute task!  2017-12-14 23:26:36exec time:1000execute task!  2017-12-14 23:26:38

scheduleAtFixedRate示例

System.out.println("start time: " + dateFormatter.format(new Date()));new Timer().scheduleAtFixedRate(new TimerTask(){    int i = 0;    int slpTimes[] = new int[] {2000,4000,100,100,100};    public void run() {        try {            System.out.println("execute task:" + i + "!  "+ dateFormatter.format(this.scheduledExecutionTime()) + " now time: " + dateFormatter.format(new Date())) ;            int slpTime = slpTimes[i++%slpTimes.length];            System.out.println("exec time:" + slpTime);            Thread.sleep(slpTime);        } catch (InterruptedException e) {            e.printStackTrace();           }      }},startDate, 2 * 1000);

输出结果

start time: 2017-12-15 00:00:09                                     //开始执行时间未到定时execute task:0!  2017-12-15 00:01:00 now time: 2017-12-15 00:01:00 //定时开始执行exec time:2000                                                      //刚好执行2sexecute task:1!  2017-12-15 00:01:02 now time: 2017-12-15 00:01:02 //所以第二次在规定时间执行exec time:4000                                                      //第二次执行2s,导致第三次延迟执行(第三次应该在2017-12-15 00:01:04执行)execute task:2!  2017-12-15 00:01:04 now time: 2017-12-15 00:01:06 //第三次在2017-12-15 00:01:06执行exec time:100                                                       //第三次执行100msexecute task:3!  2017-12-15 00:01:06 now time: 2017-12-15 00:01:06 //因为之前导致了延迟,需要追赶,所以立即执行,以下类似 exec time:100execute task:4!  2017-12-15 00:01:08 now time: 2017-12-15 00:01:08exec time:100execute task:5!  2017-12-15 00:01:10 now time: 2017-12-15 00:01:10exec time:2000execute task:6!  2017-12-15 00:01:12 now time: 2017-12-15 00:01:12

参考
schedule和scheduleAtFixedRate区别
Timer的schedule和scheduleAtFixedRate方法的区别解析