DelayQueue达到定时触发效果

DelayQueue的特点就是插入Queue中的数据可以按照自定义的delay时间进行排序。只有delay时间小于0的元素才能够被取出。

这样子,只要开启一个线程循环从DelayQueue中取值执行,就可以达到想要的效果

定义

  1. 执行的任务类

    public abstract class Task implements Delayed, Runnable{
    private String id = "";
    private long start = 0;
    /**
    * @param id id
    * @param delayInMilliseconds 延时时间(单位:毫秒)
    */
    public Task(String id, long delayInMilliseconds){
    this.id = id;
    this.start = System.currentTimeMillis() + delayInMilliseconds;
    }
    public String getId() {
    return id;
    }
    @Override
    public long getDelay(TimeUnit unit) {
    long diff = this.start - System.currentTimeMillis();
    return unit.convert(diff, TimeUnit.MILLISECONDS);
    }
    @Override
    public int compareTo(Delayed o) {
    return Ints.saturatedCast(this.start - ((Task) o).start);
    }
    @Override
    public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null) return false;
    if (!(o instanceof Task)) {
    return false;
    }
    Task t = (Task)o;
    return this.id.equals(t.getId());
    }
    @Override
    public int hashCode() {
    return this.id.hashCode();
    }
    }
  2. 任务处理类

    @Component
    public class TaskService {
    private TaskService taskService;
    // Task是自定义任务类,public abstract class Task implements Delayed, Runnable
    private DelayQueue<Task> delayQueue = new DelayQueue<Task>();
    // 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次
    @PostConstruct
    private void init() {
    taskService = this;
    // 开线程死循环执行任务
    new Thread(() -> {
    while (true) {
    try {
    Runnable task = delayQueue.take();
    task.run();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }).start();
    }
    // 添加任务
    public void addTask(Task task){
    if(delayQueue.contains(task)){
    return;
    }
    delayQueue.add(task);
    }
    public void removeTask(Task task){
    delayQueue.remove(task);
    }
    }

使用

  1. 新建自定义的任务类,需要继承Task

    private class DemoTask extends Task {
    DemoTask(String id, long delayInMilliseconds){
    super(id, delayInMilliseconds);
    }
    @Override
    public void run() {
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String now = df.format(LocalDateTime.now());
    System.out.println("task id=" + this.getId() + " at time=" + now);
    }
    }
  2. 使用

    taskService.addTask(new DemoTask("1", 3000));

代码来源:

https://github.com/linlinjava/litemall

posted @   王谷雨  阅读(104)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示