博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java基础学习之Timer定时器使用
阅读量:4045 次
发布时间:2019-05-24

本文共 9872 字,大约阅读时间需要 32 分钟。

  最近需要用到定时调用的功能。可以通过java的Timer类来进行定时调用,下面是有关Timer的一些相关知识。

  其实就Timer来讲就是一个调度器,而TimerTask呢只是一个实现了run方法的一个类,而具体的TimerTask需要由你自己来实现,例如这样:

Timer timer = new Timer();timer.schedule(new TimerTask() {        public void run() {            System.out.println("11232");        }}, 200000 , 1000);

  这里直接实现一个TimerTask(当然,你可以实现多个TimerTask,多个TimerTask可以被一个Timer会被分配到多个 Timer中被调度,后面会说到Timer的实现机制就是说内部的调度机制),然后编写run方法,20s后开始执行,每秒执行一次,当然你通过一个 timer对象来操作多个timerTask,其实timerTask本身没什么意义,只是和timer集合操作的一个对象,实现它就必然有对应的run 方法,以被调用,他甚至于根本不需要实现Runnable,因为这样往往混淆视听了,为什么呢?也是本文要说的重点。

  在说到timer的原理时,我们先看看Timer里面的一些常见方法:

1、这个方法是调度一个task,经过delay(ms)后开始进行调度,仅仅调度一次。

public void schedule(TimerTask task, long delay)

2、在指定的时间点time上调度一次。

public void schedule(TimerTask task, Date time)

3、这个方法是调度一个task,在delay(ms)后开始调度,每次调度完后,最少等待period(ms)后才开始调度。

public void schedule(TimerTask task, long delay, long period)

4、和上一个方法类似,唯一的区别就是传入的第二个参数为第一次调度的时间。

public void schedule(TimerTask task, Date firstTime, long period)

5、调度一个task,在delay(ms)后开始调度,然后每经过period(ms)再次调度,貌似和方法:schedule是一样的,其实不然,后面你会根据源码看到,schedule在计算下一次执行的时间的时候,是通过当前时间(在任务执行前得到) + 时间片,而scheduleAtFixedRate方法是通过当前需要执行的时间(也就是计算出现在应该执行的时间)+ 时间片,前者是运行的实际时间,而后者是理论时间点,例如:schedule时间片是5s,那么理论上会在5、10、15、20这些时间片被调度,但是如果由于某些CPU征用导致未被调度,假如等到第8s才被第一次调度,那么schedule方法计算出来的下一次时间应该是第13s而不是第10s,这样有可能下次就越到20s后而被少调度一次或多次,而scheduleAtFixedRate方法就是每次理论计算出下一次需要调度的时间用以排序,若第8s被调度,那么计算出应该是第10s,所以它距离当前时间是2s,那么再调度队列排序中,会被优先调度,那么就尽量减少漏掉调度的情况。 

public void scheduleAtFixedRate(TimerTask task, long delay, long period)

6、方法同上,唯一的区别就是第一次调度时间设置为一个Date时间,而不是当前时间的一个时间片,我们在源码中会详细说明这些内容。

public void scheduleAtFixedRate(TimerTask task, Date firstTime,long period)

 

源码部分

首先看Timer的构造方法有几种:

构造方法1无参构造方法,简单通过Tiemer为前缀构造一个线程名称:

public Timer() {    this("Timer-" + serialNumber());}

 创建的线程不为主线程,则主线程结束后,timer自动结束,而无需使用cancel来完成对timer的结束。

 

构造方法2传入了是否为后台线程,后台线程当且仅当进程结束时,自动注销掉。

public Timer(boolean isDaemon) {    this("Timer-" + serialNumber(), isDaemon);}   另外两个构造方法负责传入名称和将timer启动:public Timer(String name, boolean isDaemon) {      thread.setName(name);      thread.setDaemon(isDaemon);      thread.start();  }

   这里有一个thread,这个thread很明显是一个线程,被包装在了Timer类中,我们看下这个thread的定义是:

private TimerThread thread = new TimerThread(queue);

  而定义TimerThread部分的是:

class TimerThread extends Thread {    /**     * This flag is set to false by the reaper to inform us that there     * are no more live references to our Timer object.  Once this flag     * is true and there are no more tasks in our queue, there is no     * work left for us to do, so we terminate gracefully.  Note that     * this field is protected by queue's monitor!     */    boolean newTasksMayBeScheduled = true;    /**     * Our Timer's queue.  We store this reference in preference to     * a reference to the Timer so the reference graph remains acyclic.     * Otherwise, the Timer would never be garbage-collected and this     * thread would never go away.     */    private TaskQueue queue;    TimerThread(TaskQueue queue) {        this.queue = queue;    }    public void run() {        try {            mainLoop();        } finally {            // Someone killed this Thread, behave as if Timer cancelled            synchronized(queue) {                newTasksMayBeScheduled = false;                queue.clear();  // Eliminate obsolete references            }        }    }    /**     * The main timer loop.  (See class comment.)     */    private void mainLoop() {        while (true) {            try {                TimerTask task;                boolean taskFired;                synchronized(queue) {                    // Wait for queue to become non-empty                    while (queue.isEmpty() && newTasksMayBeScheduled)                        queue.wait();                    if (queue.isEmpty())                        break; // Queue is empty and will forever remain; die                    // Queue nonempty; look at first evt and do the right thing                    long currentTime, executionTime;                    task = queue.getMin();                    synchronized(task.lock) {                        if (task.state == TimerTask.CANCELLED) {                            queue.removeMin();                            continue;  // No action required, poll queue again                        }                        currentTime = System.currentTimeMillis();                        executionTime = task.nextExecutionTime;                        if (taskFired = (executionTime<=currentTime)) {                            if (task.period == 0) { // Non-repeating, remove                                queue.removeMin();                                task.state = TimerTask.EXECUTED;                            } else { // Repeating task, reschedule                                queue.rescheduleMin(                                  task.period<0 ? currentTime   - task.period                                                : executionTime + task.period);                            }                        }                    }                    if (!taskFired) // Task hasn't yet fired; wait                        queue.wait(executionTime - currentTime);                }                if (taskFired)  // Task fired; run it, holding no locks                    task.run();            } catch(InterruptedException e) {            }        }    }}

   看到这里知道了,Timer内部包装了一个线程,用来做独立于外部线程的调度,而TimerThread是一个default类型的,默认情况下是引用不到的,是被Timer自己所使用的。

 

接下来看下有那些属性

  除了上面提到的thread,还有一个很重要的属性是:

private TaskQueue queue = new TaskQueue();

   看名字就知道是一个队列,队列里面可以先猜猜看是什么,那么大概应该是我要调度的任务吧,先记录下了,接下来继续向下看:

  里面还有一个属性是:threadReaper, 它是Object类型,只是重写了finalize方法而已,是为了垃圾回收的时候,将相应的信息回收掉,做GC的回补,也就是当timer线程由于某种 原因死掉了,而未被cancel,里面的队列中的信息需要清空掉,不过我们通常是不会考虑这个方法的,所以知道java写这个方法是干什么的就行了。

  接下来看调度方法的实现:

  对于上面6个调度方法,我们不做一一列举,为什么等下你就知道了:

  来看下方法:

public void schedule(TimerTask task, long delay)

   的源码如下:

1 public void schedule(TimerTask task, long delay) {2        if (delay < 0)3            throw new IllegalArgumentException("Negative delay.");4        sched(task, System.currentTimeMillis()+delay, 0);5    }

   这里调用了另一个方法,将task传入,第一个参数传入System.currentTimeMillis()+delay可见为第一次需要执行的时间的 时间点了(如果传入Date,就是对象.getTime()即可,所以传入Date的几个方法就不用多说了),而第三个参数传入了0,这里可以猜下要么是 时间片,要么是次数啥的,不过等会就知道是什么了;另外关于方法:sched的内容我们不着急去看他,先看下重载的方法中是如何做的

  再看看方法:

public void schedule(TimerTask task, long delay,long period)   源码为:public void schedule(TimerTask task, long delay, long period) {        if (delay < 0)            throw new IllegalArgumentException("Negative delay.");        if (period <= 0)            throw new IllegalArgumentException("Non-positive period.");        sched(task, System.currentTimeMillis()+delay, -period);    }

   看来也调用了方法sched来完成调度,和上面的方法唯一的调度时候的区别是增加了传入的period,而第一个传入的是0,所以确定这个参数为时间片, 而不是次数,注意这个里的period加了一个负数,也就是取反,也就是我们开始传入1000,在调用sched的时候会变成-1000,其实最终阅读完 源码后你会发现这个算是老外对于一种数字的理解,而并非有什么特殊的意义,所以阅读源码的时候也有这些困难所在。

  最后再看个方法是:

public void scheduleAtFixedRate(TimerTasktask,long delay,long period)   源码为:public void scheduleAtFixedRate(TimerTask task, long delay, long period) {       if (delay < 0)           throw new IllegalArgumentException("Negative delay.");       if (period <= 0)           throw new IllegalArgumentException("Non-positive period.");       sched(task, System.currentTimeMillis()+delay, period);   }

   唯一的区别就是在period没有取反,其实你最终阅读完源码,上面的取反没有什么特殊的意义,老外不想增加一个参数来表示 scheduleAtFixedRate,而scheduleAtFixedRate和schedule的大部分逻辑代码一致,因此用了参数的范围来作为 区分方法,也就是当你传入的参数不是正数的时候,你调用schedule方法正好是得到scheduleAtFixedRate的功能,而调用 scheduleAtFixedRate方法的时候得到的正好是schedule方法的功能,呵呵,这些讨论没什么意义,讨论实质和重点:

   来看sched方法的实现体:

private void sched(TimerTask task, long time, long period) {        if (time < 0)            throw new IllegalArgumentException("Illegal execution time.");         synchronized(queue) {            if (!thread.newTasksMayBeScheduled)                throw new IllegalStateException("Timer already cancelled.");             synchronized(task.lock) {                if (task.state != TimerTask.VIRGIN)                    throw new IllegalStateException(                        "Task already scheduled or cancelled");                task.nextExecutionTime = time;                task.period = period;                task.state = TimerTask.SCHEDULED;            }             queue.add(task);            if (queue.getMin() == task)                queue.notify();        }    }

   queue为一个队列,我们先不看他数据结构,看到他在做这个操作的时候,发生了同步,所以在timer级别,这个是线程安全的,最后将task相关的参数赋值,主要包含nextExecutionTime(下一次执行时间),period(时间片),state(状态),然后将它放入queue队列中,做一次notify操作,为什么要做notify操作呢?看了后面的代码你就知道了。

 

  简言之,这里就是讲task放入队列queue的过程,此时,你可能对queue的结构有些兴趣,那么我们先来看看queue属性的结构TaskQueue:

class TaskQueue {     private TimerTask[] queue = new TimerTask[128];     private int size = 0;

   可见,TaskQueue的结构很简单,为一个数组,加一个size,有点像ArrayList,是不是长度就128呢,当然不 是,ArrayList可以扩容,它可以,只是会造成内存拷贝而已,所以一个Timer来讲,只要内部的task个数不超过128是不会造成扩容的;内部 提供了add(TimerTask)、size()、getMin()、get(int)、removeMin()、quickRemove(int)、 rescheduleMin(long newTime)、isEmpty()、clear()、fixUp()、fixDown()、heapify();


实践部分

1、通过继承TimerTask的方式实现

  必须重写run方法.

public class MyTask extends TimerTask{    @Override    public void run()    {        SimpleDateFormat sdf = null;        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");        System.out.println("当前时间:" + sdf.format(new Date()));            }    }public class TestTask{    public static void main(String[] args)    {        Timer t = new Timer(); // 建立Timer对象        MyTask task = new MyTask(); //定义任务        t.schedule(task, 1000,2000);//设置任务的执行,1秒后开始,每2秒执行一次                Calendar cal = Calendar.getInstance();        cal.set(Calendar.MINUTE, 30);                t.schedule(task, cal.getTime() , 2000);            }}
2、通过匿名内部类实现
Timer timer = new Timer();          timer.scheduleAtFixedRate(new TimerTask() {                  public void run() {                                          System.out.println("abc");                  }          }, 1000 , 1000);

 

转载地址:http://thwci.baihongyu.com/

你可能感兴趣的文章
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
DIV/CSS:一个贴在左上角的标签
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
<转>文档视图指针互获
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>
关于进制转换的具体实现代码
查看>>