java多线程之定时器Timer

在java中,定时计划任务功能主要使用的就是Timer对象,主要有如下技术点:

  • 实现指定事件执行指定任务
  • 实现按照指定周期执行任务

定时器Timer

schedule(TimerTask task,Date time)

该方法的作用是咋爱指定的日期执行一次某一任务。这里分两种情况
- 执行任务的任务晚于当前时间,在未来执行
- 执行的时间早于当前时间,提前运行

在未来执行
public class TimerTest {

    private static class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            System.out.println("MyTimerTask runs and the time is : "+new Date());
        }
    }

    public static void main(String[] args) throws ParseException {
        MyTimerTask myTimerTask = new MyTimerTask();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 这里设置的时间是在未来,此时时间到达该点的时候,会执行
        String dateString = "2016-07-17 08:09:00";
        Date date = sdf.parse(dateString);
        Timer timer = new Timer();
        timer.schedule(myTimerTask, date);
    }
}

多个TimerTask任务延时测试

由于TimerTask是以队列的方式一个一个被顺序执行的,所以执行时间可能和预期的时间不一致,如果前面的任务消耗时间较长,则后面的任务运行的时间也会被延迟。

public class TimerTest {

    private static class MyTimerTask1 extends TimerTask {
        @Override
        public void run() {
            try {
                System.out.println("MyTimerTask1 begin runs and the time is : "+new Date());
                Thread.sleep(30000);
                System.out.println("MyTimerTask1 end runs and the time is : "+new Date());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static class MyTimerTask2 extends TimerTask {
        @Override
        public void run() {
            System.out.println("MyTimerTask2 runs and the time is : "+new Date());
        }
    }

    public static void main(String[] args) throws ParseException {
        MyTimerTask1 myTimerTask1 = new MyTimerTask1();
        MyTimerTask2 myTimerTask2 = new MyTimerTask2();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString1 = "2016-07-17 08:27:00";// MyTimerTask1的起始执行时间
        String dateString2 = "2016-07-17 08:27:05";// MyTimerTask2的起始执行时间
        Date date1 = sdf.parse(dateString1);
        Date date2 = sdf.parse(dateString2);
        Timer timer = new Timer();
        timer.schedule(myTimerTask1, date1);
        timer.schedule(myTimerTask2, date2);
    }
}

此时程序运行结果如下:
这里写图片描述

可以看到这里由于MyTimerTask1 执行需要花费30秒,那么将会影响task2的执行时间,task2以此时间为基准,向后延迟执行,因为task是被放入到队列中,一个一个顺序执行的。

schedule(TimerTask task,Date firstTime,long period)

该方法的作用是在指定的日期之后,按照指定的间隔周期性的无限循环执行某一任务。

MyTimerTask1 myTimerTask1 = new MyTimerTask1();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString1 = "2016-07-17 08:51:00";// MyTimerTask1的起始执行时间
        Date date1 = sdf.parse(dateString1);
        Timer timer = new Timer();
        timer.schedule(myTimerTask1, date1,2000);

这里写图片描述

cancel()

  • TimerTask类的cancel()方法:将自身从任务队列中清除
  • Timer类的cancel()方法:全部人物都会被清除,并且进程被销毁

scheduleAtFixedRate(TimerTask task,Date firstTime,long period)

schedule和scheduleAtFixedRate主要的区别只在于不延时的情况:

  • schedule方法:如果执行的任务时间没有被延时,那么下一次任务的执行时间参考的是上一次任务的开始时间来计算
  • scheduleAtFixedRate方法:如果执行任务的时间没有被延时,那么下一次任务的执行时间参考的是上一次任务结束的事件来计算

chedule方法任务不延时

public class TimerTest {

    private static Timer timer = new Timer();
    private static class MyTimerTask1 extends TimerTask {

        private int count = 0;

        @Override
        public void run() {
            try {
                System.out.println("MyTimerTask1 begin runs and the time is : "+new Date());
                Thread.sleep(1000);
                System.out.println("MyTimerTask1 end runs and the time is : "+new Date());
                count++;
                if (count > 3) {
                    timer.cancel();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws ParseException {
        MyTimerTask1 myTimerTask1 = new MyTimerTask1();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = "2016-07-24 09:56:00";// MyTimerTask1的起始执行时间
        Date date = sdf.parse(dateString);
        timer.schedule(myTimerTask1, date,3000); //从2016-07-24 09:56:00开始,每隔3秒执行一次
    }
}

这里,我们的时间间隔是大于task的执行时间的,也就是说,schedule方法任务不延时,此时,打印的结果如下:
这里写图片描述

schedule方法任务延时

这里,修改schedule方法任务延时,只需要,让运行时间隔的时间小于任务的执行时间即可,代码如下:
这里写图片描述
运行结果:
这里写图片描述

scheduleAtFixedRate方法任务延时

这里只需要修改上面的schedule方法为scheduleAtFixedRate即可,此时运行程序代码有如下输出:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值