• Java多线程(十四):Timer


    Timer

    schedule(TimerTask task, Date time)

    该方法在指定日期执行任务,如果是过去的时间,这个任务会立即被执行。
    执行时间早于当前时间
    示例代码,当前时间是2019年9月19日,代码中写的是前一天的时间。

    public class MyTask1 extends TimerTask {
        private static Timer timer = new Timer();
    
        public void run()
        {
            System.out.println("运行了!时间为:" + new Date());
        }
    
        public static void main(String[] args) throws Exception
        {
            MyTask1 task = new MyTask1();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = "2019-9-18 21:22:00";
            Date dateRef = sdf.parse(dateString);
            System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:" + new Date().toLocaleString());
            timer.schedule(task, dateRef);
        }
    }
    

    运行结果如下

    字符串时间:2019-9-18 21:22:00 当前时间:2019-9-19 20:18:26
    运行了!时间为:Thu Sep 19 20:18:26 CEST 2019
    

    可以看到,过去的时间立即执行。
    执行时间晚于当前时间
    修改代码的dateString改为未来的时间,如"2019-9-19 20:22:00"
    运行结果如下

    字符串时间:2019-9-19 20:22:00 当前时间:2019-9-19 20:21:22
    运行了!时间为:Thu Sep 19 20:22:00 CEST 2019
    

    可以看到,未来的时间要等到目标时间才会执行。
    多个Timer同时执行
    示例代码如下

    public class MyTask2 extends TimerTask {
        private static Timer timer = new Timer();
    
        public void run()
        {
            System.out.println("运行了!时间为:" + new Date());
        }
    
        public static void main(String[] args) throws Exception
        {
            MyTask2 task1 = new MyTask2();
            MyTask2 task2 = new MyTask2();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString1 = "2019-9-19 21:12:00";
            String dateString2 = "2019-9-19 21:12:00";
            Date dateRef1 = sdf1.parse(dateString1);
            Date dateRef2 = sdf2.parse(dateString2);
            System.out.println("字符串时间:" + dateRef1.toLocaleString() + " 当前时间:" + new Date().toLocaleString());
            System.out.println("字符串时间:" + dateRef2.toLocaleString() + " 当前时间:" + new Date().toLocaleString());
            timer.schedule(task1, dateRef1);
            timer.schedule(task2, dateRef2);
        }
    }
    

    运行结果如下

    字符串时间:2019-9-19 21:12:00 当前时间:2019-9-19 21:11:57
    字符串时间:2019-9-19 21:12:00 当前时间:2019-9-19 21:11:57
    运行了!时间为:Thu Sep 19 21:12:00 CEST 2019
    运行了!时间为:Thu Sep 19 21:12:00 CEST 2019
    

    说明可以多任务执行。执行时间和当前时间的关系规则同上。

    schedule(TimerTask task, Date firstTime, long period)

    该方法在指定的时间执行任务,间隔period时间再次执行,无限循环。
    执行时间早于当前时间
    示例代码

    public class MyTask3 extends TimerTask {
        public void run()
        {
            System.out.println("运行了!时间为:" + new Date());
        }
    
        public static void main(String[] args) throws Exception
        {
            MyTask3 task = new MyTask3();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = "2019-09-19 22:01:00";
            Timer timer = new Timer();
            Date dateRef = sdf.parse(dateString);
            System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:" + new Date().toLocaleString());
            timer.schedule(task, dateRef, 4000);
        }
    }
    

    运行结果如下

    字符串时间:2019-9-19 22:01:00 当前时间:2019-9-19 22:09:13
    运行了!时间为:Thu Sep 19 22:09:13 CEST 2019
    运行了!时间为:Thu Sep 19 22:09:17 CEST 2019
    运行了!时间为:Thu Sep 19 22:09:21 CEST 2019
    运行了!时间为:Thu Sep 19 22:09:25 CEST 2019
    ···
    

    可以看到,立即执行。
    执行时间晚于当前时间
    运行结果如下

    字符串时间:2019-9-19 22:12:00 当前时间:2019-9-19 22:11:24
    运行了!时间为:Thu Sep 19 22:12:00 CEST 2019
    运行了!时间为:Thu Sep 19 22:12:04 CEST 2019
    运行了!时间为:Thu Sep 19 22:12:08 CEST 2019
    运行了!时间为:Thu Sep 19 22:12:12 CEST 2019
    运行了!时间为:Thu Sep 19 22:12:16 CEST 2019
    ···
    
  • 相关阅读:
    Linux安装配置nginx
    Linux下apache安装php
    Linux安装配置apache
    Linux安装mysql
    安装Linux CentOS与用Xshell实现远程连接
    关于IIS上Yii2的Url路由美化
    安装android Studio和运行react native项目(跳坑篇)
    安装android Studio和运行react native项目(基础篇)
    第10章 同步设备I/O和异步设备I/O(3)_接收I/O请求完成通知的4种方法
    第10章 同步设备I/O和异步设备I/O(2)_同步IO和异步IO基础
  • 原文地址:https://www.cnblogs.com/Java-Starter/p/11553611.html
Copyright © 2020-2023  润新知