Java 定时器的多种实现方式

作者:卷卷啊 时间:2021-06-14 03:15:09 

目录
  • 一、前言

    • (1)Timer

    • (2)DelayedQueue 延迟队列

    • (3)ScheduledThreadPoolExecutor

    • (4)ScheduledThreadPoolExecutor

一、前言

定时器有三种表现形式:

  • 按固定周期定时执行

  • 延迟一定时间后执行

  • 指定某个时刻执行

JDK 提供了三种常用的定时器实现方式,分别为:

  • Timer

  • DelayedQueue 延迟队列

  • ScheduledThreadPoolExecutor

(1)Timer

发现 eureka 中大量使用了 Timer 定时器:

  • Timer 属于 JDK 比较早期版本的实现,它可以实现固定周期的任务,以及延迟任务。

  • Timer 会起动一个异步线程去执行到期的任务,任务可以只被调度执行一次,也可以周期性反复执行多次。

Timer 是如何使用的,示例代码如下:


Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
   @Override
   public void run() {
       // 业务代码
   }
}, 5000, 5000);  // 5s 后调度一个周期为 5s 的定时任务
  • TimerTask 是实现了 Runnable 接口的抽象类

  • Timer 负责调度和执行 TimerTask

Timer 的内部构造,如下:


public class Timer {
   // 小根堆,run操作 O(1)、新增 O(logn)、cancel O(logn)
   private final TaskQueue queue = new TaskQueue();
   // 创建另外线程,任务处理,会轮询 queue
   private final TimerThread thread = new TimerThread(queue);
   public Timer(String name) {
       thread.setName(name);
       thread.start();
   }
}

Timer 它是存在不少设计缺陷的,所以并不推荐用户使用:

  • Timer 是单线程模式,如果某个 TimerTask 执行时间很久,会影响其他任务的调度。

  • Timer 的任务调度是基于系统绝对时间的,如果系统时间不正确,可能会出现问题。

  • TimerTask 如果执行出现异常,Timer 并不会捕获,会导致线程终止,其他任务永远不会执行。

(2)DelayedQueue 延迟队列

特征如下:

  • DelayedQueue 是 JDK 中一种可以延迟获取对象的阻塞队列,其内部是采用优先级队列 PriorityQueue 存储对象

  • DelayQueue 中的每个对象都必须实现 Delayed 接口,并重写 compareTogetDelay 方法

DelayedQueue 的使用方法如下:


public class DelayQueueTest {
   public static void main(String[] args) throws Exception {
       BlockingQueue<SampleTask> delayQueue = new DelayQueue<>();
       long now = System.currentTimeMillis();
       delayQueue.put(new SampleTask(now + 1000));
       delayQueue.put(new SampleTask(now + 2000));
       delayQueue.put(new SampleTask(now + 3000));
       for (int i = 0; i < 3; i++) {
           System.out.println(new Date(delayQueue.take().getTime()));
       }
   }
   static class SampleTask implements Delayed {
       long time;
       public SampleTask(long time) {
           this.time = time;
       }
       public long getTime() {
           return time;
       }
       @Override
       public int compareTo(Delayed o) {
           return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));
       }
       @Override
       public long getDelay(TimeUnit unit) {
           return unit.convert(time - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
       }
   }
}

(3)ScheduledThreadPoolExecutor

JDK 提供了功能更加丰富的 ScheduledThreadPoolExecutor


public class ScheduledExecutorServiceTest {
   public static void main(String[] args) {
       ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
       executor.scheduleAtFixedRate(() -> System.out.println("Hello World"), 1000, 2000, TimeUnit.MILLISECONDS); // 1s 延迟后开始执行任务,每 2s 重复执行一次
   }
}

ScheduledThreadPoolExecutor 使用了阻塞队列 DelayedWorkQueue

(4)ScheduledThreadPoolExecutor

线程应该是最常见的实现方案,创建一个线程执行任务即可,举例几个不同的写法,代码如下

4.1.使用thread + runnable


package com.yezi_tool.demo_basic.test;

import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ThreadTest {

private Integer count = 0;

public ThreadTest() {
       test1();
   }

public void test1() {
       new Thread(() -> {
           while (count < 10) {
               System.out.println(new Date().toString() + ": " + count);
               count++;
               try {
                   Thread.sleep(3000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       }).start();
   }
}

4.2.使用线程池 + runnable


package com.yezi_tool.demo_basic.test;

import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class ThreadTest {

private static final ExecutorService threadPool = Executors.newFixedThreadPool(5);// 线程池
   private Integer count = 0;

public ThreadTest() {
       test2();
   }

public void test2() {
       threadPool.execute(() -> {
           while (count < 10) {
               System.out.println(new Date().toString() + ": " + count);
               count++;
               try {
                   Thread.sleep(3000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       });
   }
}

来源:https://juejin.cn/post/6970339917700268040

标签:Java,定时器
0
投稿

猜你喜欢

  • 详解Kotlin中的面向对象(一)

    2023-08-17 12:57:38
  • Spring自动装配之方法、构造器位置的自动注入操作

    2021-11-30 23:28:40
  • 浅析Java中Map与HashMap,Hashtable,HashSet的区别

    2022-05-31 13:46:57
  • C# 使用HttpClient模拟请求的案例

    2023-10-16 18:20:14
  • Java中常见的对象转换工具

    2023-12-14 19:23:16
  • Java开发利器之Guava Cache的使用教程

    2022-03-20 19:22:02
  • 基于Java实现的Dijkstra算法示例

    2021-09-17 02:51:13
  • 解决response.setHeader设置下载文件名无效的问题

    2021-08-15 20:43:54
  • arthas jprofiler做复杂链路的调用分析

    2022-01-15 12:01:25
  • IDEA JeeSite框架httpSession.invalidate()无效问题解决方案

    2023-09-23 19:44:24
  • Java DWR内存泄漏问题解决方案

    2022-02-28 02:35:07
  • java多线程编程学习(线程间通信)

    2023-04-02 05:25:34
  • Java多线程的实现方式比较(两种方式比较)

    2023-06-06 13:00:12
  • C#实现飞行棋源码

    2023-11-09 11:17:09
  • SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤

    2023-03-31 03:38:11
  • 单例模式 分析代码优化方法

    2021-07-28 15:49:51
  • SpringBoot使用swagger生成api接口文档的方法详解

    2021-10-22 18:11:48
  • 解决java执行cmd命令调用ffmpeg报错Concat error - No such filter '[0,0]'问题

    2023-03-14 20:35:11
  • C#实现将记事本中的代码编译成可执行文件的方法

    2022-03-05 02:17:53
  • 一篇文章弄懂JVM类加载机制过程以及原理

    2022-01-19 10:32:57
  • asp之家 软件编程 m.aspxhome.com