定时任务实现原理详解

开发 前端
在很多业务的系统中,我们常常需要定时的执行一些任务,例如定时发短信、定时变更数据、定时发起促销活动等等。

 本文转载自微信公众号「Java极客技术」,作者鸭血粉丝。转载本文请联系Java极客技术公众号。  

一、摘要

在很多业务的系统中,我们常常需要定时的执行一些任务,例如定时发短信、定时变更数据、定时发起促销活动等等。

在上篇文章中,我们简单的介绍了定时任务的使用方式,不同的架构对应的解决方案也有所不同,总结起来主要分单机和分布式两大类,本文会重点分析下单机的定时任务实现原理以及优缺点,分布式框架的实现原理会在后续文章中进行分析。

从单机角度,定时任务实现主要有以下 3 种方案:

  • while + sleep 组合
  • 最小堆实现
  • 时间轮实现

二、while+sleep组合

while+sleep 方案,简单的说,就是定义一个线程,然后 while 循环,通过 sleep 延迟时间来达到周期性调度任务。

简单示例如下:

  1. public static void main(String[] args) { 
  2.     final long timeInterval = 5000; 
  3.     new Thread(new Runnable() { 
  4.         @Override 
  5.         public void run() { 
  6.             while (true) { 
  7.                 System.out.println(Thread.currentThread().getName() + "每隔5秒执行一次"); 
  8.                 try { 
  9.                     Thread.sleep(timeInterval); 
  10.                 } catch (InterruptedException e) { 
  11.                     e.printStackTrace(); 
  12.                 } 
  13.             } 
  14.         } 
  15.     }).start(); 

实现上非常简单,如果我们想在创建一个每隔3秒钟执行一次任务,怎么办呢?

同样的,也可以在创建一个线程,然后间隔性的调度方法;但是如果创建了大量这种类型的线程,这个时候会发现大量的定时任务线程在调度切换时性能消耗会非常大,而且整体效率低!

面对这种在情况,大佬们也想到了,于是想出了用一个线程将所有的定时任务存起来,事先排好序,按照一定的规则来调度,这样不就可以极大的减少每个线程的切换消耗吗?

正因此,JDK 中的 Timer 定时器由此诞生了!

三、最小堆实现

所谓最小堆方案,正如我们上面所说的,每当有新任务加入的时候,会把需要即将要执行的任务排到前面,同时会有一个线程不断的轮询判断,如果当前某个任务已经到达执行时间点,就会立即执行,具体实现代表就是 JDK 中的 Timer 定时器!

3.1、Timer

首先我们来一个简单的 Timer 定时器例子

  1. public static void main(String[] args) { 
  2.     Timer timer = new Timer(); 
  3.     //每隔1秒调用一次 
  4.     timer.schedule(new TimerTask() { 
  5.         @Override 
  6.         public void run() { 
  7.             System.out.println("test1"); 
  8.         } 
  9.     }, 1000, 1000); 
  10.     //每隔3秒调用一次 
  11.     timer.schedule(new TimerTask() { 
  12.         @Override 
  13.         public void run() { 
  14.             System.out.println("test2"); 
  15.         } 
  16.     }, 3000, 3000); 
  17.  

实现上,好像跟我们上面介绍的 while+sleep 方案差不多,同样也是起一个TimerTask线程任务,只不过共用一个Timer调度器。

下面我们一起来打开源码看看里面到底有些啥!

  • 进入Timer.schedule()方法

从方法上可以看出,这里主要做参数验证,其中TimerTask是一个线程任务,delay表示延迟多久执行(单位毫秒),period表示多久执行一次(单位毫秒)

  1. public void schedule(TimerTask task, long delay, long period) { 
  2.     if (delay < 0) 
  3.         throw new IllegalArgumentException("Negative delay."); 
  4.     if (period <= 0) 
  5.         throw new IllegalArgumentException("Non-positive period."); 
  6.     sched(task, System.currentTimeMillis()+delay, -period); 
  • 接着看sched()方法

这步操作中,可以很清晰的看到,在同步代码块里,会将task对象加入到queue

  1. private void sched(TimerTask task, long time, long period) { 
  2.     if (time < 0) 
  3.         throw new IllegalArgumentException("Illegal execution time."); 
  4.  
  5.     // Constrain value of period sufficiently to prevent numeric 
  6.     // overflow while still being effectively infinitely large. 
  7.     if (Math.abs(period) > (Long.MAX_VALUE >> 1)) 
  8.         period >>= 1; 
  9.  
  10.     synchronized(queue) { 
  11.         if (!thread.newTasksMayBeScheduled) 
  12.             throw new IllegalStateException("Timer already cancelled."); 
  13.  
  14.         synchronized(task.lock) { 
  15.             if (task.state != TimerTask.VIRGIN) 
  16.                 throw new IllegalStateException( 
  17.                     "Task already scheduled or cancelled"); 
  18.             task.nextExecutionTime = time
  19.             task.period = period; 
  20.             task.state = TimerTask.SCHEDULED; 
  21.         } 
  22.  
  23.         queue.add(task); 
  24.         if (queue.getMin() == task) 
  25.             queue.notify(); 
  26.     } 
  • 我们继续来看queue对象

任务会将入到TaskQueue队列中,同时在Timer初始化阶段会将TaskQueue作为参数传入到TimerThread线程中,并且起到线程

  1. public class Timer { 
  2.      
  3.     private final TaskQueue queue = new TaskQueue(); 
  4.  
  5.     private final TimerThread thread = new TimerThread(queue); 
  6.  
  7.     public Timer() { 
  8.         this("Timer-" + serialNumber()); 
  9.     } 
  10.  
  11.     public Timer(String name) { 
  12.         thread.setName(name); 
  13.         thread.start(); 
  14.     } 
  15.  
  16.     //... 
  • 而TaskQueue其实是一个最小堆的数据实体类,源码如下

每当有新元素加入的时候,会对原来的数组进行重排,会将即将要执行的任务排在数组的前面

  1. class TaskQueue { 
  2.      
  3.     private TimerTask[] queue = new TimerTask[128]; 
  4.  
  5.  
  6.     private int size = 0; 
  7.  
  8.     void add(TimerTask task) { 
  9.         // Grow backing store if necessary 
  10.         if (size + 1 == queue.length) 
  11.             queue = Arrays.copyOf(queue, 2*queue.length); 
  12.  
  13.         queue[++size] = task; 
  14.         fixUp(size); 
  15.     } 
  16.  
  17.     private void fixUp(int k) { 
  18.         while (k > 1) { 
  19.             int j = k >> 1; 
  20.             if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime) 
  21.                 break; 
  22.             TimerTask tmp = queue[j]; 
  23.    queue[j] = queue[k]; 
  24.    queue[k] = tmp; 
  25.             k = j; 
  26.         } 
  27.     } 
  28.   
  29.  //.... 
  • 最后我们来看看TimerThread

TimerThread其实就是一个任务调度线程,首先从TaskQueue里面获取排在最前面的任务,然后判断它是否到达任务执行时间点,如果已到达,就会立刻执行任务

  1. class TimerThread extends Thread { 
  2.  
  3.     boolean newTasksMayBeScheduled = true
  4.  
  5.     private TaskQueue queue; 
  6.  
  7.     TimerThread(TaskQueue queue) { 
  8.         this.queue = queue; 
  9.     } 
  10.  
  11.     public void run() { 
  12.         try { 
  13.             mainLoop(); 
  14.         } finally { 
  15.             // Someone killed this Thread, behave as if Timer cancelled 
  16.             synchronized(queue) { 
  17.                 newTasksMayBeScheduled = false
  18.                 queue.clear();  // Eliminate obsolete references 
  19.             } 
  20.         } 
  21.     } 
  22.  
  23.     /** 
  24.      * The main timer loop.  (See class comment.) 
  25.      */ 
  26.     private void mainLoop() { 
  27.         while (true) { 
  28.             try { 
  29.                 TimerTask task; 
  30.                 boolean taskFired; 
  31.                 synchronized(queue) { 
  32.                     // Wait for queue to become non-empty 
  33.                     while (queue.isEmpty() && newTasksMayBeScheduled) 
  34.                         queue.wait(); 
  35.                     if (queue.isEmpty()) 
  36.                         break; // Queue is empty and will forever remain; die 
  37.  
  38.                     // Queue nonempty; look at first evt and do the right thing 
  39.                     long currentTime, executionTime; 
  40.                     task = queue.getMin(); 
  41.                     synchronized(task.lock) { 
  42.                         if (task.state == TimerTask.CANCELLED) { 
  43.                             queue.removeMin(); 
  44.                             continue;  // No action required, poll queue again 
  45.                         } 
  46.                         currentTime = System.currentTimeMillis(); 
  47.                         executionTime = task.nextExecutionTime; 
  48.                         if (taskFired = (executionTime<=currentTime)) { 
  49.                             if (task.period == 0) { // Non-repeating, remove 
  50.                                 queue.removeMin(); 
  51.                                 task.state = TimerTask.EXECUTED; 
  52.                             } else { // Repeating task, reschedule 
  53.                                 queue.rescheduleMin( 
  54.                                   task.period<0 ? currentTime   - task.period 
  55.                                                 : executionTime + task.period); 
  56.                             } 
  57.                         } 
  58.                     } 
  59.                     if (!taskFired) // Task hasn't yet fired; wait 
  60.                         queue.wait(executionTime - currentTime); 
  61.                 } 
  62.                 if (taskFired)  // Task fired; run it, holding no locks 
  63.                     task.run(); 
  64.             } catch(InterruptedException e) { 
  65.             } 
  66.         } 
  67.     } 

总结这个利用最小堆实现的方案,相比 while + sleep 方案,多了一个线程来管理所有的任务,优点就是减少了线程之间的性能开销,提升了执行效率;但是同样也带来的了一些缺点,整体的新加任务写入效率变成了 O(log(n))。

同时,细心的发现,这个方案还有以下几个缺点:

  • 串行阻塞:调度线程只有一个,长任务会阻塞短任务的执行,例如,A任务跑了一分钟,B任务至少需要等1分钟才能跑
  • 容错能力差:没有异常处理能力,一旦一个任务执行故障,后续任务都无法执行

3.2、ScheduledThreadPoolExecutor

鉴于 Timer 的上述缺陷,从 Java 5 开始,推出了基于线程池设计的 ScheduledThreadPoolExecutor 。

其设计思想是,每一个被调度的任务都会由线程池来管理执行,因此任务是并发执行的,相互之间不会受到干扰。需要注意的是,只有当任务的执行时间到来时,ScheduledThreadPoolExecutor 才会真正启动一个线程,其余时间 ScheduledThreadPoolExecutor 都是在轮询任务的状态。

简单的使用示例:

  1. public static void main(String[] args) { 
  2.     ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3); 
  3.     //启动1秒之后,每隔1秒执行一次 
  4.     executor.scheduleAtFixedRate((new Runnable() { 
  5.         @Override 
  6.         public void run() { 
  7.             System.out.println("test3"); 
  8.         } 
  9.     }),1,1, TimeUnit.SECONDS); 
  10.     //启动1秒之后,每隔3秒执行一次 
  11.     executor.scheduleAtFixedRate((new Runnable() { 
  12.         @Override 
  13.         public void run() { 
  14.             System.out.println("test4"); 
  15.         } 
  16.     }),1,3, TimeUnit.SECONDS); 

同样的,我们首先打开源码,看看里面到底做了啥

  • 进入scheduleAtFixedRate()方法

首先是校验基本参数,然后将任务作为封装到ScheduledFutureTask线程中,ScheduledFutureTask继承自RunnableScheduledFuture,并作为参数调用delayedExecute()方法进行预处理

  1. public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, 
  2.                                               long initialDelay, 
  3.                                               long period, 
  4.                                               TimeUnit unit) { 
  5.     if (command == null || unit == null
  6.         throw new NullPointerException(); 
  7.     if (period <= 0) 
  8.         throw new IllegalArgumentException(); 
  9.     ScheduledFutureTask<Void> sft = 
  10.         new ScheduledFutureTask<Void>(command, 
  11.                                       null
  12.                                       triggerTime(initialDelay, unit), 
  13.                                       unit.toNanos(period)); 
  14.     RunnableScheduledFuture<Void> t = decorateTask(command, sft); 
  15.     sft.outerTask = t; 
  16.     delayedExecute(t); 
  17.     return t; 
  • 继续看delayedExecute()方法

可以很清晰的看到,当线程池没有关闭的时候,会通过super.getQueue().add(task)操作,将任务加入到队列,同时调用ensurePrestart()方法做预处理

  1. private void delayedExecute(RunnableScheduledFuture<?> task) { 
  2.     if (isShutdown()) 
  3.         reject(task); 
  4.     else { 
  5.         super.getQueue().add(task); 
  6.         if (isShutdown() && 
  7.             !canRunInCurrentRunState(task.isPeriodic()) && 
  8.             remove(task)) 
  9.             task.cancel(false); 
  10.         else 
  11.    //预处理 
  12.             ensurePrestart(); 
  13.     } 

其中super.getQueue()得到的是一个自定义的new DelayedWorkQueue()阻塞队列,数据存储方面也是一个最小堆结构的队列,这一点在初始化new ScheduledThreadPoolExecutor()的时候,可以看出!

  1. public ScheduledThreadPoolExecutor(int corePoolSize) { 
  2.     super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, 
  3.           new DelayedWorkQueue()); 

打开源码可以看到,DelayedWorkQueue其实是ScheduledThreadPoolExecutor中的一个静态内部类,在添加的时候,会将任务加入到RunnableScheduledFuture数组中,同时线程池中的Woker线程会通过调用任务队列中的take()方法获取对应的ScheduledFutureTask线程任务,接着执行对应的任务线程

  1. static class DelayedWorkQueue extends AbstractQueue<Runnable> 
  2.         implements BlockingQueue<Runnable> { 
  3.  
  4.     private static final int INITIAL_CAPACITY = 16; 
  5.     private RunnableScheduledFuture<?>[] queue = 
  6.         new RunnableScheduledFuture<?>[INITIAL_CAPACITY]; 
  7.     private final ReentrantLock lock = new ReentrantLock(); 
  8.     private int size = 0;    
  9.  
  10.     //.... 
  11.  
  12.     public boolean add(Runnable e) { 
  13.         return offer(e); 
  14.     } 
  15.  
  16.     public boolean offer(Runnable x) { 
  17.         if (x == null
  18.             throw new NullPointerException(); 
  19.         RunnableScheduledFuture<?> e = (RunnableScheduledFuture<?>)x; 
  20.         final ReentrantLock lock = this.lock; 
  21.         lock.lock(); 
  22.         try { 
  23.             int i = size
  24.             if (i >= queue.length) 
  25.                 grow(); 
  26.             size = i + 1; 
  27.             if (i == 0) { 
  28.                 queue[0] = e; 
  29.                 setIndex(e, 0); 
  30.             } else { 
  31.                 siftUp(i, e); 
  32.             } 
  33.             if (queue[0] == e) { 
  34.                 leader = null
  35.                 available.signal(); 
  36.             } 
  37.         } finally { 
  38.             lock.unlock(); 
  39.         } 
  40.         return true
  41.     } 
  42.  
  43.     public RunnableScheduledFuture<?> take() throws InterruptedException { 
  44.         final ReentrantLock lock = this.lock; 
  45.         lock.lockInterruptibly(); 
  46.         try { 
  47.             for (;;) { 
  48.                 RunnableScheduledFuture<?> first = queue[0]; 
  49.                 if (first == null
  50.                     available.await(); 
  51.                 else { 
  52.                     long delay = first.getDelay(NANOSECONDS); 
  53.                     if (delay <= 0) 
  54.                         return finishPoll(first); 
  55.                     first = null; // don't retain ref while waiting 
  56.                     if (leader != null
  57.                         available.await(); 
  58.                     else { 
  59.                         Thread thisThread = Thread.currentThread(); 
  60.                         leader = thisThread; 
  61.                         try { 
  62.                             available.awaitNanos(delay); 
  63.                         } finally { 
  64.                             if (leader == thisThread) 
  65.                                 leader = null
  66.                         } 
  67.                     } 
  68.                 } 
  69.             } 
  70.         } finally { 
  71.             if (leader == null && queue[0] != null
  72.                 available.signal(); 
  73.             lock.unlock(); 
  74.         } 
  75.     } 
  • 回到我们最开始说到的ScheduledFutureTask任务线程类,最终执行任务的其实就是它

ScheduledFutureTask任务线程,才是真正执行任务的线程类,只是绕了一圈,做了很多包装,run()方法就是真正执行定时任务的方法。

  1. private class ScheduledFutureTask<V> 
  2.             extends FutureTask<V> implements RunnableScheduledFuture<V> { 
  3.  
  4.     /** Sequence number to break ties FIFO */ 
  5.     private final long sequenceNumber; 
  6.  
  7.     /** The time the task is enabled to execute in nanoTime units */ 
  8.     private long time
  9.  
  10.     /** 
  11.      * Period in nanoseconds for repeating tasks.  A positive 
  12.      * value indicates fixed-rate execution.  A negative value 
  13.      * indicates fixed-delay execution.  A value of 0 indicates a 
  14.      * non-repeating task. 
  15.      */ 
  16.     private final long period; 
  17.  
  18.     /** The actual task to be re-enqueued by reExecutePeriodic */ 
  19.     RunnableScheduledFuture<V> outerTask = this; 
  20.  
  21.     /** 
  22.      * Overrides FutureTask version so as to reset/requeue if periodic. 
  23.      */ 
  24.     public void run() { 
  25.         boolean periodic = isPeriodic(); 
  26.         if (!canRunInCurrentRunState(periodic)) 
  27.             cancel(false); 
  28.         else if (!periodic) 
  29.             ScheduledFutureTask.super.run(); 
  30.         else if (ScheduledFutureTask.super.runAndReset()) { 
  31.             setNextRunTime(); 
  32.             reExecutePeriodic(outerTask); 
  33.         } 
  34.     } 
  35.   
  36.  //... 

3.3、小结

ScheduledExecutorService 相比 Timer 定时器,完美的解决上面说到的 Timer 存在的两个缺点!

在单体应用里面,使用 ScheduledExecutorService 可以解决大部分需要使用定时任务的业务需求!

但是这是否意味着它是最佳的解决方案呢?

我们发现线程池中 ScheduledExecutorService 的排序容器跟 Timer 一样,都是采用最小堆的存储结构,新任务加入排序效率是O(log(n)),执行取任务是O(1)。

这里的写入排序效率其实是有空间可提升的,有可能优化到O(1)的时间复杂度,也就是我们下面要介绍的时间轮实现!

四、时间轮实现

所谓时间轮(RingBuffer)实现,从数据结构上看,简单的说就是循环队列,从名称上看可能感觉很抽象。

它其实就是一个环形的数组,如图所示,假设我们创建了一个长度为 8 的时间轮。

插入、取值流程:

  • 1.当我们需要新建一个 1s 延时任务的时候,则只需要将它放到下标为 1 的那个槽中,2、3、...、7也同样如此。
  • 2.而如果是新建一个 10s 的延时任务,则需要将它放到下标为 2 的槽中,但同时需要记录它所对应的圈数,也就是 1 圈,不然就和 2 秒的延时消息重复了
  • 3.当创建一个 21s 的延时任务时,它所在的位置就在下标为 5 的槽中,同样的需要为他加上圈数为 2,依次类推...

因此,总结起来有两个核心的变量:

  • 数组下标:表示某个任务延迟时间,从数据操作上对执行时间点进行取余
  • 圈数:表示需要循环圈数

通过这张图可以更直观的理解!

当我们需要取出延时任务时,只需要每秒往下移动这个指针,然后取出该位置的所有任务即可,取任务的时间消耗为O(1)。

当我们需要插入任务式,也只需要计算出对应的下表和圈数,即可将任务插入到对应的数组位置中,插入任务的时间消耗为O(1)。

如果时间轮的槽比较少,会导致某一个槽上的任务非常多,那么效率也比较低,这就和 HashMap 的 hash 冲突是一样的,因此在设计槽的时候不能太大也不能太小。

4.1、代码实现

  • 首先创建一个RingBufferWheel时间轮定时任务管理器
  1. public class RingBufferWheel { 
  2.  
  3.     private Logger logger = LoggerFactory.getLogger(RingBufferWheel.class); 
  4.  
  5.  
  6.     /** 
  7.      * default ring buffer size 
  8.      */ 
  9.     private static final int STATIC_RING_SIZE = 64; 
  10.  
  11.     private Object[] ringBuffer; 
  12.  
  13.     private int bufferSize; 
  14.  
  15.     /** 
  16.      * business thread pool 
  17.      */ 
  18.     private ExecutorService executorService; 
  19.  
  20.     private volatile int size = 0; 
  21.  
  22.     /*** 
  23.      * task stop sign 
  24.      */ 
  25.     private volatile boolean stop = false
  26.  
  27.     /** 
  28.      * task start sign 
  29.      */ 
  30.     private volatile AtomicBoolean start = new AtomicBoolean(false); 
  31.  
  32.     /** 
  33.      * total tick times 
  34.      */ 
  35.     private AtomicInteger tick = new AtomicInteger(); 
  36.  
  37.     private Lock lock = new ReentrantLock(); 
  38.     private Condition condition = lock.newCondition(); 
  39.  
  40.     private AtomicInteger taskId = new AtomicInteger(); 
  41.     private Map<Integer, Task> taskMap = new ConcurrentHashMap<>(16); 
  42.  
  43.     /** 
  44.      * Create a new delay task ring buffer by default size 
  45.      * 
  46.      * @param executorService the business thread pool 
  47.      */ 
  48.     public RingBufferWheel(ExecutorService executorService) { 
  49.         this.executorService = executorService; 
  50.         this.bufferSize = STATIC_RING_SIZE; 
  51.         this.ringBuffer = new Object[bufferSize]; 
  52.     } 
  53.  
  54.  
  55.     /** 
  56.      * Create a new delay task ring buffer by custom buffer size 
  57.      * 
  58.      * @param executorService the business thread pool 
  59.      * @param bufferSize      custom buffer size 
  60.      */ 
  61.     public RingBufferWheel(ExecutorService executorService, int bufferSize) { 
  62.         this(executorService); 
  63.  
  64.         if (!powerOf2(bufferSize)) { 
  65.             throw new RuntimeException("bufferSize=[" + bufferSize + "] must be a power of 2"); 
  66.         } 
  67.         this.bufferSize = bufferSize; 
  68.         this.ringBuffer = new Object[bufferSize]; 
  69.     } 
  70.  
  71.     /** 
  72.      * Add a task into the ring buffer(thread safe) 
  73.      * 
  74.      * @param task business task extends {@link Task} 
  75.      */ 
  76.     public int addTask(Task task) { 
  77.         int key = task.getKey(); 
  78.         int id; 
  79.  
  80.         try { 
  81.             lock.lock(); 
  82.             int index = mod(key, bufferSize); 
  83.             task.setIndex(index); 
  84.             Set<Task> tasks = get(index); 
  85.  
  86.             int cycleNum = cycleNum(key, bufferSize); 
  87.             if (tasks != null) { 
  88.                 task.setCycleNum(cycleNum); 
  89.                 tasks.add(task); 
  90.             } else { 
  91.                 task.setIndex(index); 
  92.                 task.setCycleNum(cycleNum); 
  93.                 Set<Task> sets = new HashSet<>(); 
  94.                 sets.add(task); 
  95.                 put(key, sets); 
  96.             } 
  97.             id = taskId.incrementAndGet(); 
  98.             task.setTaskId(id); 
  99.             taskMap.put(id, task); 
  100.             size++; 
  101.         } finally { 
  102.             lock.unlock(); 
  103.         } 
  104.  
  105.         start(); 
  106.  
  107.         return id; 
  108.     } 
  109.  
  110.  
  111.     /** 
  112.      * Cancel task by taskId 
  113.      * @param id unique id through {@link #addTask(Task)} 
  114.      * @return 
  115.      */ 
  116.     public boolean cancel(int id) { 
  117.  
  118.         boolean flag = false
  119.         Set<Task> tempTask = new HashSet<>(); 
  120.  
  121.         try { 
  122.             lock.lock(); 
  123.             Task task = taskMap.get(id); 
  124.             if (task == null) { 
  125.                 return false
  126.             } 
  127.  
  128.             Set<Task> tasks = get(task.getIndex()); 
  129.             for (Task tk : tasks) { 
  130.                 if (tk.getKey() == task.getKey() && tk.getCycleNum() == task.getCycleNum()) { 
  131.                     size--; 
  132.                     flag = true
  133.                     taskMap.remove(id); 
  134.                 } else { 
  135.                     tempTask.add(tk); 
  136.                 } 
  137.  
  138.             } 
  139.             //update origin data 
  140.             ringBuffer[task.getIndex()] = tempTask; 
  141.         } finally { 
  142.             lock.unlock(); 
  143.         } 
  144.  
  145.         return flag; 
  146.     } 
  147.  
  148.     /** 
  149.      * Thread safe 
  150.      * 
  151.      * @return the size of ring buffer 
  152.      */ 
  153.     public int taskSize() { 
  154.         return size
  155.     } 
  156.  
  157.     /** 
  158.      * Same with method {@link #taskSize} 
  159.      * @return 
  160.      */ 
  161.     public int taskMapSize(){ 
  162.         return taskMap.size(); 
  163.     } 
  164.  
  165.     /** 
  166.      * Start background thread to consumer wheel timer, it will always run until you call method {@link #stop} 
  167.      */ 
  168.     public void start() { 
  169.         if (!start.get()) { 
  170.  
  171.             if (start.compareAndSet(start.get(), true)) { 
  172.                 logger.info("Delay task is starting"); 
  173.                 Thread job = new Thread(new TriggerJob()); 
  174.                 job.setName("consumer RingBuffer thread"); 
  175.                 job.start(); 
  176.                 start.set(true); 
  177.             } 
  178.  
  179.         } 
  180.     } 
  181.  
  182.     /** 
  183.      * Stop consumer ring buffer thread 
  184.      * 
  185.      * @param force True will force close consumer thread and discard all pending tasks 
  186.      *              otherwise the consumer thread waits for all tasks to completes before closing. 
  187.      */ 
  188.     public void stop(boolean force) { 
  189.         if (force) { 
  190.             logger.info("Delay task is forced stop"); 
  191.             stop = true
  192.             executorService.shutdownNow(); 
  193.         } else { 
  194.             logger.info("Delay task is stopping"); 
  195.             if (taskSize() > 0) { 
  196.                 try { 
  197.                     lock.lock(); 
  198.                     condition.await(); 
  199.                     stop = true
  200.                 } catch (InterruptedException e) { 
  201.                     logger.error("InterruptedException", e); 
  202.                 } finally { 
  203.                     lock.unlock(); 
  204.                 } 
  205.             } 
  206.             executorService.shutdown(); 
  207.         } 
  208.  
  209.  
  210.     } 
  211.  
  212.  
  213.     private Set<Task> get(int index) { 
  214.         return (Set<Task>) ringBuffer[index]; 
  215.     } 
  216.  
  217.     private void put(int keySet<Task> tasks) { 
  218.         int index = mod(key, bufferSize); 
  219.         ringBuffer[index] = tasks; 
  220.     } 
  221.  
  222.     /** 
  223.      * Remove and get task list. 
  224.      * @param key 
  225.      * @return task list 
  226.      */ 
  227.     private Set<Task> remove(int key) { 
  228.         Set<Task> tempTask = new HashSet<>(); 
  229.         Set<Task> result = new HashSet<>(); 
  230.  
  231.         Set<Task> tasks = (Set<Task>) ringBuffer[key]; 
  232.         if (tasks == null) { 
  233.             return result; 
  234.         } 
  235.  
  236.         for (Task task : tasks) { 
  237.             if (task.getCycleNum() == 0) { 
  238.                 result.add(task); 
  239.  
  240.                 size2Notify(); 
  241.             } else { 
  242.                 // decrement 1 cycle number and update origin data 
  243.                 task.setCycleNum(task.getCycleNum() - 1); 
  244.                 tempTask.add(task); 
  245.             } 
  246.             // remove task, and free the memory. 
  247.             taskMap.remove(task.getTaskId()); 
  248.         } 
  249.  
  250.         //update origin data 
  251.         ringBuffer[key] = tempTask; 
  252.  
  253.         return result; 
  254.     } 
  255.  
  256.     private void size2Notify() { 
  257.         try { 
  258.             lock.lock(); 
  259.             size--; 
  260.             if (size == 0) { 
  261.                 condition.signal(); 
  262.             } 
  263.         } finally { 
  264.             lock.unlock(); 
  265.         } 
  266.     } 
  267.  
  268.     private boolean powerOf2(int target) { 
  269.         if (target < 0) { 
  270.             return false
  271.         } 
  272.         int value = target & (target - 1); 
  273.         if (value != 0) { 
  274.             return false
  275.         } 
  276.  
  277.         return true
  278.     } 
  279.  
  280.     private int mod(int target, int mod) { 
  281.         // equals target % mod 
  282.         target = target + tick.get(); 
  283.         return target & (mod - 1); 
  284.     } 
  285.  
  286.     private int cycleNum(int target, int mod) { 
  287.         //equals target/mod 
  288.         return target >> Integer.bitCount(mod - 1); 
  289.     } 
  290.  
  291.     /** 
  292.      * An abstract class used to implement business. 
  293.      */ 
  294.     public abstract static class Task extends Thread { 
  295.  
  296.         private int index
  297.  
  298.         private int cycleNum; 
  299.  
  300.         private int key
  301.  
  302.         /** 
  303.          * The unique ID of the task 
  304.          */ 
  305.         private int taskId ; 
  306.  
  307.         @Override 
  308.         public void run() { 
  309.         } 
  310.  
  311.         public int getKey() { 
  312.             return key
  313.         } 
  314.  
  315.         /** 
  316.          * 
  317.          * @param key Delay time(seconds) 
  318.          */ 
  319.         public void setKey(int key) { 
  320.             this.key = key
  321.         } 
  322.  
  323.         public int getCycleNum() { 
  324.             return cycleNum; 
  325.         } 
  326.  
  327.         private void setCycleNum(int cycleNum) { 
  328.             this.cycleNum = cycleNum; 
  329.         } 
  330.  
  331.         public int getIndex() { 
  332.             return index
  333.         } 
  334.  
  335.         private void setIndex(int index) { 
  336.             this.index = index
  337.         } 
  338.  
  339.         public int getTaskId() { 
  340.             return taskId; 
  341.         } 
  342.  
  343.         public void setTaskId(int taskId) { 
  344.             this.taskId = taskId; 
  345.         } 
  346.     } 
  347.  
  348.  
  349.     private class TriggerJob implements Runnable { 
  350.  
  351.         @Override 
  352.         public void run() { 
  353.             int index = 0; 
  354.             while (!stop) { 
  355.                 try { 
  356.                     Set<Task> tasks = remove(index); 
  357.                     for (Task task : tasks) { 
  358.                         executorService.submit(task); 
  359.                     } 
  360.  
  361.                     if (++index > bufferSize - 1) { 
  362.                         index = 0; 
  363.                     } 
  364.  
  365.                     //Total tick number of records 
  366.                     tick.incrementAndGet(); 
  367.                     TimeUnit.SECONDS.sleep(1); 
  368.  
  369.                 } catch (Exception e) { 
  370.                     logger.error("Exception", e); 
  371.                 } 
  372.  
  373.             } 
  374.  
  375.             logger.info("Delay task has stopped"); 
  376.         } 
  377.     } 
  • 接着,编写一个客户端,测试客户端
  1. public static void main(String[] args) { 
  2.     RingBufferWheel ringBufferWheel = new RingBufferWheel( Executors.newFixedThreadPool(2)); 
  3.     for (int i = 0; i < 3; i++) { 
  4.         RingBufferWheel.Task job = new Job(); 
  5.         job.setKey(i); 
  6.         ringBufferWheel.addTask(job); 
  7.     } 
  8.  
  9. public static class Job extends RingBufferWheel.Task{ 
  10.     @Override 
  11.     public void run() { 
  12.         System.out.println("test5"); 
  13.     } 

运行结果:

  1. test5 
  2. test5 
  3. test5 

如果要周期性执行任务,可以在任务执行完成之后,再重新加入到时间轮中。

详细源码分析地址:[https://crossoverjie.top/2019/09/27/algorithm/time%20wheel/]

4.2、应用

时间轮的应用还是非常广的,例如在 Disruptor 项目中就运用到了 RingBuffer,还有Netty中的HashedWheelTimer工具原理也差不多等等,有兴趣的同学,可以阅读一下官方对应的源码!

五、小结

本文主要围绕单体应用中的定时任务原理进行分析,可能也有理解不对的地方,欢迎批评指出!

六、参考

1、简书 - 谈谈定时任务解决方案原理

2、crossoverJie's Blog - 延时消息之时间轮

 

责任编辑:武晓燕 来源: Java极客技术
相关推荐

2021-06-30 07:19:34

SpringBoot定时任务

2019-12-25 15:10:00

MySQL事件数据库

2023-12-19 08:09:06

Python定时任务Cron表达式

2020-12-10 07:00:38

编程线程池定时任务

2017-08-16 16:41:04

JavaSpringBoot定时任务

2024-01-22 08:53:00

策略任务RocketMQ

2021-11-22 12:35:40

Python命令定时任务

2024-01-31 08:38:57

Python定时任务函数

2024-02-26 11:12:33

定时任务线程

2012-02-07 13:31:14

SpringJava

2009-10-28 10:05:29

Ubuntucrontab定时任务

2010-03-10 15:47:58

crontab定时任务

2024-02-28 09:54:07

线程池配置

2023-12-11 09:50:35

Linux定时器

2010-01-07 13:38:41

Linux定时任务

2009-06-22 13:30:00

定时任务Java

2019-02-25 15:44:16

开源RabbitMQSpring Clou

2010-06-02 17:31:10

MySQL定时任务

2021-12-16 14:25:03

Linux定时任务

2022-08-15 15:43:29

Linuxcron
点赞
收藏

51CTO技术栈公众号