Java定时器Timer学习笔记

1、 定时任务调度
基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务。

2、 Java中的定时任务调度工具:Timer和Quartz
Timer和Quartz区别:
1) Timer由JDK自身提供
2) Quartz时间控制功能更强大
3) 底层实现机制,Timer只有一个后台线程去执行定时任务,而Quartz拥有后台执行线程池,可以使用多个线程去执行定时任务。

3、 Timer的定义
有且仅有一个后台线程对多个业务线程进行定时定频率的调度。Timer可以简单理解为调度线程,即后台线程,TimerTask为业务线程。Timer定时调用TimerTask。
这里写图片描述
这里写图片描述

4、 Timer小Demo

public class MyTimerTask extends TimerTask{

    private String name;

    public MyTimerTask(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println("Current exec name is: " + name);
    }
}

public class MyTimer {
    public static void main(String[] args){
        //1.创建一个Timer实例
        Timer timer = new Timer();
        //2.创建一个MyTimerTask实例
        MyTimerTask myTimerTask = new MyTimerTask("No.1");
        //3.通过timer定时定频率调用myTimerTask的业务逻辑
        //即第一次执行是在当前时间的两秒之后,之后每隔一秒钟执行一次
        timer.schedule(myTimerTask, 2000L, 1000L);
    }

}

5、 schedule的4种用法和scheduleAtFixedRate两种用法

public class MyTimerTask extends TimerTask{
    private String name;

    public MyTimerTask(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Time is: " + df.format(calendar.getTime()));
        System.out.println("My name is: " + name);
    }
}


public class MyTimer {

    public static void main(String[] args) {
        /** 1.创建一个Timer实例 **/
        Timer timer = new Timer();

        /** 2.创建一个MyTimerTask实例 **/
        MyTimerTask task = new MyTimerTask("lwh sayHello");

        /** 3.通过timer定时定频率调用task **/
        //timer.schedule(task, 2000L, 1000L);

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(calendar.getTime()));
        calendar.add(Calendar.SECOND, 3);

        /**
           schedule用法1
           public void schedule(TimerTask task, Date time)
           task任务,time首次执行任务的时间
           在时间等于或超过time的时候执行且仅执行一次task,如果所传时间小于当前时间立即执行
         */
        //timer.schedule(task, calendar.getTime());

        /**
           schedule用法2
           public void schedule(TimerTask task, Date firstTime, long period)
           task任务,firstTime首次执行任务的时间,period执行一次task的时间间隔,单位毫秒
           在时间等于或超过time的时候首次执行task,之后每隔period毫秒重复执行一次task
         */
        //timer.schedule(task, calendar.getTime(), 2000L);

        /**
           schedule用法3
           public void schedule(TimerTask task, long delay)
           task任务,delay延迟时间
           等待delay毫秒之后执行且仅执行一次task
         */
        //timer.schedule(task, 1000L);

        /**
           schedule用法4
           public void schedule(TimerTask task, long delay, long period)
           task任务,delay延迟时间,period执行一次task的时间间隔
           等待delay毫秒之后首次执行task,之后每隔period毫秒重复执行一次task
         */
        //timer.schedule(task, 1000L, 3000L);

        /**
           scheduleAtFixedRate用法1
           public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
           task任务,firstTime首次执行任务的时间,period执行一次task的时间间隔,单位毫秒
           在时间等于或超过time的时候首次执行task,之后每隔period毫秒重复执行一次task
         */
        //timer.scheduleAtFixedRate(task, calendar.getTime(), 2000L);

        /**
           scheduleAtFixedRate用法2
           public void scheduleAtFixedRate(TimerTask task, long delay, long period)
           task任务,delay延迟时间,period执行一次task的时间间隔
           等待delay毫秒之后首次执行task,之后每隔period毫秒重复执行一次task
         */
        timer.scheduleAtFixedRate(task, 2000L, 3000L);
    }
}

6、 TimerTask的两个重要函数
1) cancel() 执行3次后取消定时任务

public class MyTimerTask extends TimerTask{
    private String name;

    private Integer count = 0;

    public MyTimerTask(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        if(count < 3){
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("Current Time is: " + df.format(calendar.getTime()));
            System.out.println("My name is: " + name);
            count++;
        }else {
            /** 取消当前TimerTask里的任务 **/
            cancel();
            System.out.println("Task cancel");
        }

    }
}

2)scheduledExecutionTime() 返回此任务最近实际执行的已安排执行时间

timer.schedule(task, 4000L);
System.out.println("schedule time is: " + df.format(task.scheduledExecutionTime()));

这里写图片描述

7、 Timer的其他重要函数
1) cancel() 终止此计时器,丢弃所有当前已安排的任务

2) purge() 从此计时器的任务队列中移除所有已取消的任务,返回从队列中移除的任务数

8、 schedule和scheduleAtFixedRate两种情况看区别
1) 首次计划执行的时间早于当前时间

public class ScheduleDiff {

    public static void main(String[] args) {
        final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();

        System.out.println("Current time is: " + df.format(calendar.getTime()));

        /** 设置成六秒之前的时间,若当前时间为2018-12-28 00:00:06,
            那么设置之后的时间变成2018- 12-28 00:00:00 **/
        calendar.add(Calendar.SECOND, -6);

        Timer timer = new Timer();

        /** 第一次执行时间为6秒之前,随后每隔两秒执行一次 **/
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                /** 打印当前的计划执行时间 **/
                System.out.println("Scheduled exec time is: " + df.format(scheduledExecutionTime()));

                System.out.println("Task is being executed!");
            }
        }, calendar.getTime(), 2000);
    }
}

这里写图片描述

public class ScheduleFixRateDiff {

    public static void main(String[] args) {
        final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();

        System.out.println("Current time is: " + df.format(calendar.getTime()));

        /** 设置成六秒之前的时间,若当前时间为2018-12-28 00:00:06,
            那么设置之后的时间变成2018-12-28 00:00:00 **/
        calendar.add(Calendar.SECOND, -6);

        Timer timer = new Timer();
        /** 第一次执行时间为6秒之前,随后每隔两秒执行一次 **/
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                /** 打印当前的计划执行时间 **/
                System.out.println("Scheduled exec time is: " + df.format(scheduledExecutionTime()));
                System.out.println("Task is being executed!");
            }
        }, calendar.getTime(), 2000);

    }
}

这里写图片描述

2) 任务所需的执行时间超出任务的执行间隔周期
schedule方法:下一次执行时间相对于上次实际执行完成的时间点,因此执行时间会不断延后。看下面例子发现时间间隔其实变成了3秒。

public class ScheduleDiff {

    public static void main(String[] args) {
        final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();

        System.out.println("Current time is: " + df.format(calendar.getTime()));
        Timer timer = new Timer();
        /** 第一次执行时间为6秒之前,随后每隔两秒执行一次 **/
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try{
                    /** 任务执行时间3秒 **/
                    Thread.sleep(3000);
                }catch (InterruptedException e){

                }
                /** 打印当前的计划执行时间 **/
                System.out.println("Scheduled exec time is: " + df.format(scheduledExecutionTime()));
                System.out.println("Task is being executed!");
            }
        }, calendar.getTime(), 2000);

    }
}

这里写图片描述

ScheduleAtFixedRate方法:下一次执行时间相对于上一次开始的时间点,因此执行时间一般不会延后,存在并发性。

public class ScheduleFixRateDiff {

    public static void main(String[] args) {
        final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();

        System.out.println("Current time is: " + df.format(calendar.getTime()));

        Timer timer = new Timer();
        /** 第一次执行时间为6秒之前,随后每隔两秒执行一次 **/
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                try{
                    /** 任务执行时间3秒 **/
                    Thread.sleep(3000);
                }catch (InterruptedException e){

                }

                /** 打印当前的计划执行时间 **/
                System.out.println("Scheduled exec time is: " + df.format(scheduledExecutionTime()));
                System.out.println("Task is being executed!");
            }
        }, calendar.getTime(), 2000);

    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值