定时任务实现方式

今天和大家分享一篇干货内容,相信大家在日常生活中经常会有看见定时i删除,定时刷新,定时重置这类型的功能,那么这些往往都是由我们今天的主角,定时任务所实现,开讲!

定时任务

首先,我们从简单开始,先讲Java中的定时任务实现方式

1.Java实现定时任务的方式

一、线程等待/线程休眠(不建议使用,任务复杂时存在内存泄露风险)

Thread myThread = new Thread(new Runnable() {
   
    @Override
    public void run() {
   
        while (true) {
   
            System.out.println("TestThreadWait is called!");
            try {
   
                // 使用线程休眠来实现周期执行
                Thread.sleep(1000 * 3);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
        }
    }
});
myThread.start();

二、Timer定时器实现简单的定时任务

package com.gt.test;

import java.util.Timer;
import java.util.TimerTask;

public class Main {
   
    public static void main(String[] args) {
   
        /*Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("执行定时任务");
            }
        },1000,1000);*/

        TimerTask task = new TimerTask() {
   
            @Override
            public void run() {
   
                System.out.println("TimerTask is called!");
            }
        };

        Timer timer = new Timer();
        /*
         * schedule 和 scheduleAtFixedRate 区别:
         *  可将schedule理解为scheduleAtFixedDelay,
         *  两者主要区别在于delay和rate
         *  1、schedule,如果第一次执行被延时(delay),
         *      随后的任务执行时间将以上一次任务实际执行完成的时间为准
         *  2、scheduleAtFixedRate,如果第一次执行被延时(delay),
         *      随后的任务执行时间将以上一次任务开始执行的时间为准(需考虑同步)
         *
         *  参数:1、任务体    2、延时时间(可以指定执行日期)3、任务执行间隔时间
         */
        // timer.schedule(task, 0, 1000 * 3);
        timer.scheduleAtFixedRate(task, 0, 1000 * 3);
    }
}

三、ScheduledExecutorService

简单说明:

ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便。

package com.gt.test;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {
   
    public static void main(String[] args) {
   
        Runnable runnable = new Runnable() {
   
            public void run() {
   
                System.out.println("ScheduledExecutorService Task is called!");
            }
        };
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        // 参数:1、任务体 2、首次执行的延时时间
        //      3、任务执行间隔 4、间隔时间单位
        service.scheduleAtFixedRate(runnable, 0, 3, TimeUnit.SECONDS);
    }
}

四、@Scheduled

说明:

在Spring框架中,实现定时任务很简单,常用的实现方式是使用注解@Scheduled。例如凌晨1点跑批,每10秒查询支付状态等

1、配置

在spring boot的启动类上加@EnableScheduling注解,允许支持@Scheduled:

@SpringBootApplication
@EnableScheduling
public class Quartz01Application {
   

    public static void main(String[] args) {
   
        SpringApplication.run(Quartz01Application.class, args);
    }
}
2、任务类

                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值