zoukankan      html  css  js  c++  java
  • Java定时器Timer

    Java定时器Timer
    在JDK库中,Timer类主要负责计划任务的功能,也就是在指定的时开始执行某一个任务。Timer类的主要作用就是设置计划任务,但封装任务的类却是TimerTask类,执行计划任务的代码要放入TimerTask的子类中,因为TimerTask是一个抽象类。下面通过实例说一说,如何实现指定时间执行任务以及实现指定周期执行任务。
    在指定时间执行
    方法schedule(TimerTask,Date time),该方法的作用是在指定的日期执行一次某一任务。
    执行任务类MyTask

    public class MyTask extends TimerTask{
        @Override
        public void run() {
            System.out.println("任务执行了,时间为:" + new Date());
            System.gc();    //  回收Timer
            this.cancel();   //结束当前线程
        }
    }

    场景1:执行任务晚于当前时间--延迟执行

    public class Test {
        public static void main(String[] args) {
            System.out.println("当前时间为:" + new Date());
            Calendar calen = Calendar.getInstance();
            calen.add(Calendar.SECOND,5);
            Date runDate = calen.getTime();
            MyTask task = new MyTask();
            Timer timer = new Timer();
            timer.schedule(task,runDate);
        }
    }

    执行结果:

    Connected to the target VM, address: '127.0.0.1:63242', transport: 'socket'

    当前时间为:Wed Nov 14 17:19:53 CST 2018
    任务执行了,时间为:Wed Nov 14 17:19:58 CST 2018
    Disconnected from the target VM, address: '127.0.0.1:63242', transport: 'socket'
    Process finished with exit code 0
    场景2:执行任务的时间早于当前时间--立即执行
    public class Test1 {
        public static void main(String[] args) {
            System.out.println("当前时间为:" + new Date());
            Calendar calen = Calendar.getInstance();
            calen.add(Calendar.SECOND,-10);
            Date runDate = calen.getTime();
            MyTask task = new MyTask();
            Timer timer = new Timer();
            timer.schedule(task,runDate);
        }
    }
    执行结果:
    Connected to the target VM, address: '127.0.0.1:63249', transport: 'socket'
    当前时间为:Wed Nov 14 17:20:58 CST 2018
    任务执行了,时间为:Wed Nov 14 17:20:58 CST 2018
    Disconnected from the target VM, address: '127.0.0.1:63249', transport: 'socket'
    Process finished with exit code 0
    场景3:Timer中允许有多个TimerTask
    public class Test2 {
        public static void main(String[] args) {
            System.out.println("当前时间为:" + new Date());
            Calendar calen1 = Calendar.getInstance();
            calen1.add(Calendar.SECOND,5);
            Date runDate1 = calen1.getTime();
            MyTask task1 = new MyTask();
    
            Calendar calen2 = Calendar.getInstance();
            calen2.add(Calendar.SECOND,-10);
            Date runDate2 = calen2.getTime();
            MyTask task2 = new MyTask();
    
            Timer timer = new Timer();
            timer.schedule(task1,runDate1);
            timer.schedule(task2,runDate2);
    
        }
    }
    执行结果:
    Connected to the target VM, address: '127.0.0.1:63292', transport: 'socket'
    当前时间为:Wed Nov 14 17:23:02 CST 2018
    任务执行了,时间为:Wed Nov 14 17:23:02 CST 2018
    任务执行了,时间为:Wed Nov 14 17:23:07 CST 2018
    Disconnected from the target VM, address: '127.0.0.1:63292', transport: 'socket'
    Process finished with exit code 0

    周期性执行

    方法schedule(TimerTask,Date firstTime,long period),改方法的作用是在指定的日期之后按指定的时间间隔周期,无线循环地执行某一任务。
    执行类:MyTask1
    public class MyTask1 extends TimerTask{
        @Override
        public void run() {
            System.out.println("任务执行了,时间为:" + new Date());
        }
    }
    测试类:Test3
    public class Test3 {
        public static void main(String[] args) {
            System.out.println("当前时间为:" + new Date());
            Calendar calen = Calendar.getInstance();
            calen.add(Calendar.SECOND,5);
            Date runDate = calen.getTime();
            MyTask1 task = new MyTask1();
            Timer timer = new Timer();
            timer.schedule(task,runDate,4000);
        }
    }
    执行结果:
    Connected to the target VM, address: '127.0.0.1:63382', transport: 'socket'
    当前时间为:Wed Nov 14 17:35:36 CST 2018
    任务执行了,时间为:Wed Nov 14 17:35:41 CST 2018
    任务执行了,时间为:Wed Nov 14 17:35:45 CST 2018
    任务执行了,时间为:Wed Nov 14 17:35:49 CST 2018
    任务执行了,时间为:Wed Nov 14 17:35:53 CST 2018
    任务执行了,时间为:Wed Nov 14 17:35:57 CST 2018
    注意:
    1)TimerTask类中的cancel()方法的做事是将自身从任务队列中进行清除
    2)Timer类中的cancel()方法的作用是将任务队列中的全部任务进行清空。
  • 相关阅读:
    什么是递归?先了解什么是递归.
    如何使用Nginx和uWSGI或Gunicorn在Ubuntu上部署Flask Web应用
    Android开发工具Android Studio、Android SDK和Genymotion完全配置
    自定义RatingBar评分控件
    QQ运动步数&自定义ProgressBar
    WebView动态注入JavaScript脚本
    Android IOC注解库EasyUI
    Java反射机制动态代理
    Android网络请求库RetrofitUtils
    Android MVP模式就是这么回事儿
  • 原文地址:https://www.cnblogs.com/myxcf/p/9959589.html
Copyright © 2011-2022 走看看