Java定时任务调度实战(1)深入Timer、TimerTask源码,我发现都是《数据结构》必考点

这是文章《Java原生任务调度实现及分布式任务调度平台 XXL-JOB概览》的一节,发现字数真的太多了,不利于阅读,就把这篇拆出来了。

代码中的注释在CSDN中的效果是灰色的,不容易看清,就把代码块的样式设置为了黑色主题,有点丑🐶。

目录

1.0 简单的例子

1.1 Timer的构造函数

1.2 Timer的方法

1.3 Timer的属性

1.3.1 TaskQueue

1.3.2 TimerThread

1.4 Timer总结


1.0 简单的例子

public static void main(String[] args) {
	TimerTask timerTask = new TimerTask() {
		@Override
		public void run() {
			while (true) {
		        try {
		            Thread.sleep(2000);
		        } catch (Exception e) {
		            // TODO: handle exception
		        }
		        System.out.println("TimerTask当前时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			}
		}
	};
	Timer timer = new Timer();
	long delay = 0;
	long period = 1000;
	timer.schedule(timerTask, delay, period);
}

效果:

TimerTask的原理是什么那?

实际上通过上方的示例代码你就能发现,TimerTask就是一个实现了run方法的类,TimerTask是一个抽象类,实现了Runnable,提供了抽象方法run()。

public abstract class TimerTask implements Runnable {
    //task的状态,默认是VIRGIN,共有4个状态
    int state = VIRGIN;
    
    //未执行
    static final int VIRGIN = 0;
    
	//非重复任务,未被执行
    static final int SCHEDULED   = 1;
    
	//非重复任务已执行或正执行,并且没有被取消
    static final int EXECUTED    = 2;
    
    //已经取消
    static final int CANCELLED   = 3;
    
    //下次执行时间
    long nextExecutionTime;
    
    //任务执行的周期
    long period = 0;
    
	/**
     * The action to be performed by this timer task.
     */
    public abstract void run();
    
    //这个就是把当前这个task设置为取消状态,Timer也有一个cancel方法,会面会提到
    public boolean cancel() {
        synchronized(lock) {
            boolean result = (state == SCHEDULED);
            state = CANCELLED;
            return result;
        }
    }
    
    //还有一个scheduledExecutionTime方法 就不提了
	
}

关键在于Timer 💥

1.1 Timer的构造函数

先来看一下第三个构造方法,方法体就是传入并设置线程的名字,然后启动。

public Timer(String name) {
     thread.setName(name);
     thread.start();
 }

这里的thread就是一个线程,

     /**
      * The timer thread.
      */
     private final TimerThread thread = new TimerThread(queue);

而这个TimerThread就被定义在Timer类中,从注释中可以看出,这个线程就是用来执行定时任务的具体线程,当队列中的任务被触发时执行它们。我会在1.3节中讲这个TimerThread和队列TaskQueue。😎

 /**
  * This "helper class" implements the timer's task execution thread, which
  * waits for tasks on the timer queue, executions them when they fire,
  * reschedules repeating tasks, and removes cancelled tasks and spent
  * non-repeating tasks from the queue.
  */
 class TimerThread extends Thread {
 ​
 }

此时,回过头来再看其他构造函数就一目了然了,第一个无参构造函数通过Timer为前缀名构造一个线程,里面的this就是上面这个构造函数,这里的serialNumber()就是去生成一个名字。

     public Timer() {
         this("Timer-" + serialNumber());
     }
 ​
     /**
      * This ID is used to generate thread names.
      */
     private final static AtomicInteger nextSerialNumber = new AtomicInteger(0);
     private static int serialNumber() {
         return nextSerialNumber.getAndIncrement();
     }

如果有不明白AtomicInteger()的朋友可以看着这篇博客:https://blog.csdn.net/fanrenxiang/article/details/80623884

在代码里打个断点就可以清楚的看到这个线程的名字:

第二个构造函数传入了是否为后台线程,如果是,主线程结束后会自动结束,不需要调用cancel。

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

为什么要设置为后台线程,很经典的一个例子是在web应用中使用线程,当你把web应用关闭后,这个线程还在运行!🐄🍺

这是因为线程是JVM级别的,web应用关闭后,这个线程并没有销毁。具体可以看这篇博客:https://blog.csdn.net/chetianyao8457/article/details/100924119

第四个构造函数可以设置名字及是否为后台线程,并且启动。

     public Timer(String name, boolean isDaemon) {
         thread.setName(name);
         thread.setDaemon(isDaemon);
         thread.start();
     }

好,下面开始讲Timer的方法,属性放到1.3节中

1.2 Timer的方法

timer提供了6个调度方法,其实都大同小异,先了解一下看完1.3节再回过来看

     public void schedule(TimerTask task, long delay) {
         if (delay < 0)
             throw new IllegalArgumentException("Negative delay.");
         sched(task, System.currentTimeMillis()+delay, 0);
     }
     
     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);
     }
     
     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);
     }

可以发现,最终都调用了sched方法,而schedulescheduleAtFixedRate的区别就是传入的最终period的正负数,为什么会有这种区别?看完后面就懂了。

sched方法就是对task做一些设置,加到队列里,并做一次notify操作。这些后面都会提到。

     private void sched(TimerTask task, long time, long period) {
         if (time < 0)
             throw new IllegalArgumentException("Illegal execution time.");
 ​
         // Constrain value of period sufficiently to prevent numeric
         // overflow while still being effectively infinitely large.
         if (Math.abs(period) > (Long.MAX_VALUE >> 1))
             period >>= 1;
 ​
         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;   //task状态
             }
             //添加到队列
             queue.add(task);
             if (queue.getMin() == task)
                 queue.notify();
         }
     }

cancel()方法,一旦执行,Timer就停掉了,为什么要把newTasksMayBeScheduled设置为false以及为什调用notify(),也需要看后面的讲解。

     public void cancel() {
         synchronized(queue) {
             thread.newTasksMayBeScheduled = false;
             queue.clear();
             queue.notify();  // In case queue was already empty.
         }
     }

purge()方法,当对TimerTask做了多次cancel之后,队列就混乱了,这时候就需要调用这个方法,回收空间并重新排列。

注意,这个cancel不是上面讲Timer的cancel方法,是TimerTask的cancel方法

      public int purge() {
          int result = 0;
 ​
          synchronized(queue) {
              for (int i = queue.size(); i > 0; i--) {
                  if (queue.get(i).state == TimerTask.CANCELLED) {
                      queue.quickRemove(i);
                      result++;
                  }
              }
 ​
              if (result != 0)
                  queue.heapify();
          }
 ​
          return result;
      }

1.3 Timer的属性

在上面已经提到过了Timer的两个属性thread和nextSerialNumber,此外还有一个queue,这个queue存储的就是要调度的任务,也就是TimerTask。

1.3.1 TaskQueue

TaskQueue的结构很简单,一个数组,一个size。需要注意的一点是此队列存储范围是queue[1]-queue[size]。队列默认大小是128,但,是支持扩容的。

     private final TaskQueue queue = new TaskQueue();
     
     class TaskQueue {
         private TimerTask[] queue = new TimerTask[128];
         private int size = 0;
     }

扩容的方式是在增加TimerTask时进行操作

     void add(TimerTask task) {
         // 扩容在这
         if (size + 1 == queue.length)
             queue = Arrays.copyOf(queue, 2*queue.length);
 ​
         queue[++size] = task;
         fixUp(size);
     }

TaskQueue提供了一系列的操作来对队列进行处理,尤其是排序。这里面涉及不少数据结构的操作,大学数据结构不及格的可以学一波

TaskQueue原理上是一个平衡二叉堆,

根节点的nextExecutionTime最小,queue[n]的子节点是queue[2n]和queue[2n+1],这些乱七八糟的就不说了,课本上都有。

size()、get(i)和add(TimerTask)这三个方法就不说了,getMin()是返回最近需要执行的任务,返回的就是queue[1]。

removeMin()是删除当前最近执行的任务,而删除的操作很经典,大学数据结构必学的点,就是把队尾赋给对头,队尾置为null,并且重新排序

     void removeMin() {
         queue[1] = queue[size];
         queue[size--] = null;  // Drop extra reference to prevent memory leak
         fixDown(1);
     }

fixDown(int)的作用就是下滤,不断的把queue[k]和它的子节点进行比较,直到它的nextExecutionTime小于等于子节点

     private void fixDown(int k) {
         int j;
         while ((j = k << 1) <= size && j > 0) {
             if (j < size &&
                 queue[j].nextExecutionTime > queue[j+1].nextExecutionTime)
                 j++; // j indexes smallest kid
             if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)
                 break;
             TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
             k = j;
         }
     }

fixUp(int)的作用是上溢,不断向上提升,在新增TimerTask时使用。

     private void fixUp(int k) {
         while (k > 1) {
             int j = k >> 1;
             if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)
                 break;
             TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
             k = j;
         }
     }

quickRemove(int) 删除指定的元素。为什么叫quick那,它只赋值没有排序,排序要搭配heapify()。

     void quickRemove(int i) {
         assert i <= size;
 ​
         queue[i] = queue[size];
         queue[size--] = null;  // Drop extra ref to prevent memory leak
     }
     
     void heapify() {
         //对queue的后半段做排序
         for (int i = size/2; i >= 1; i--)
             fixDown(i);
     }

rescheduleMin(newTime)重新设置当前任务的下一次执行时间并排序,为什么取queue[1]?

因为在真正执行的时候,是每次取下次执行时间最短的,也就是queue[1]。

     void rescheduleMin(long newTime) {
         queue[1].nextExecutionTime = newTime;
         fixDown(1);
     }

1.3.2 TimerThread

1.1节简单的提了下TimerThread,这里重点介绍下,因为它是Timer的重点👴👴👴

TimerThread很简单,TimerThread就是具体去执行的地方。

先来看下TimerThread的源码,主体就是mainLoop()方法。

 class TimerThread extends Thread {
     //标志位,用于在mainLoop中判断状态
     boolean newTasksMayBeScheduled = true;
     
     private TaskQueue queue;
     
     TimerThread(TaskQueue queue) {
         this.queue = queue;
     }
     
     public void run() {
         try {
             //mainLoop的具体实现,下面会讲
             mainLoop();
         } finally {
             // 将参数置为false,并且队列清空
             synchronized(queue) {
                 newTasksMayBeScheduled = false;
                 queue.clear();  // Eliminate obsolete references
             }
         }
     }
 }

那么TimerThread是如何运行的?我来给你捋一下

 //一开始我们是new了一个Timer(),通过Timer的无参构造函数配置了默认的线程名字并执行了thread.start()
 //并且通过Timer的schedule()方法把TimerTask放到队列里并设置了延迟、周期和状态。
 Timer timer = new Timer();
 timer.schedule(timerTask, delay, period);
 ​
 public Timer() {
     this("Timer-" + serialNumber());
 }
 ​
 public Timer(String name) {
     //这里的thread就是被final修饰的TimerThread,new了一个TimerThread并传递了队列queue
     //private final TimerThread thread = new TimerThread(queue);
     
     thread.setName(name);
     thread.start();
 }
 ​
 //而TimerThread是有一个TaskQueue的属性和重载的构造函数,这个重载的构造函数接收了queue
 private TaskQueue queue;
 ​
 TimerThread(TaskQueue queue) {
     this.queue = queue;
 }
 ​
 //一旦执行thread.start(),TimerThread的run方法就会执行,具体执行就在mainLoop()方法中
 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) {
         }
     }
 }
 ​
 //大体读一下这个方法,你就会发现除非遇到break或者遇到不能捕获的异常,它就是个死循环。
 ​
     while (queue.isEmpty() && newTasksMayBeScheduled)
                         queue.wait();
 ​
 //跳出循环的条件就是queue不为空或者newTasksMayBeScheduled为false
 //那么wait就是等到其他地方对queue发生nitify操作,其实就是在调用cancel的时候
 //这时标志位是false,会跳出循环,并且对queue设置了clear操作, 直接就跳出了外部的死循环。
 ​
     public void cancel() {
         synchronized(queue) {
             thread.newTasksMayBeScheduled = false;
             queue.clear();
             queue.notify();  // In case queue was already empty.
         }
     }
 ​
 //还有一处,是Timer的属性threadReaper调用finalize的时候
 //这个threadReaper只重写了finalize方法,GC的时候调用
 ​
     private final Object threadReaper = new Object() {
         protected void finalize() throws Throwable {
             synchronized(queue) {
                 thread.newTasksMayBeScheduled = false;
                 queue.notify(); // In case queue is empty.
             }
         }
     };
 //还有就是当对queue执行add操作的时候,在Timer中的sched方法,前边有提到。此时queue不为空,就跳出了循环。
 ​
 //之后是判断该task是否被取消
     if (task.state == TimerTask.CANCELLED) {
         queue.removeMin();
         continue;  // No action required, poll queue again
     }
 ​
 //再之后是取当前的系统时间和上次预计的执行时间,如果当前系统时间已经超了,就赶紧执行。
 //不过在执行之前需要判断是否是重复任务。
 //判断一下period是否为0,0就代表一次性任务,删掉。如果不是,就调用rescheduleMin设置下一次执行时间并排序。
 ​
     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);
         }
     }
 ​
 //这里很有意思,如果period是负数,下次执行时间就是当前系统时间+周期时间。如果是正数就是原计算的下次执行时间+周期时间。
 //这就是schedule和scheduleAtFixedRate的区别。换一下参数的正负数,就和另一个方法一样。。
 ​
 //再往后就是如果当前这个task执行时间还没到就等待一段时间,
     if (!taskFired) // Task hasn't yet fired; wait
         queue.wait(executionTime - currentTime);
 ​
 //如果时间到了,就执行了
     if (taskFired)  // Task fired; run it, holding no locks
         task.run();
 ​

好,至此整个执行流程就结束了,是不是很简单!😏😏😏

在回过头去看1.2节,是不是清晰又明了!👻

1.4 Timer总结

Timer可以分四个部分:

  • TimerTask是调度任务的具体内容
  • TaskQueue存放要执行的TimerTask,下标越小优先级最高。
  • TimerThread是Thread的扩展类,从TaskQueue中获取下标为1 的TimerTask执行,并根据是否是重复任务对TaskQueue进行处理。
  • Timer主要就是配置任务执行时间、间隔、执行内容,TimerThread和TaskQueue位于Timer类中。

需要注意的点是,如果要用TimerTask,一定要记得使用try catch,如果遇到不能捕获的异常Timer就终止了。

这里可以参考一个生产过程中遇到的问题:https://blog.verysu.com/article/435

欢迎关注微信公众号【刘墨泽】,吐吐槽,发发那些平台不让发的碎碎念!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值