定时任务Demo总结(推荐最后一种)
创始人
2024-05-21 05:03:12
0

方法一: 线程实现 Runnable 接口

    Thread thread = new Thread(new Runnable() {@Overridepublic void run() {while (true) {SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");String dateStr = sdf.format(new Date());System.out.println("线程等待实现定时任务:" + dateStr);try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}}}});thread.start();

测试:

在这里插入图片描述

方法二:自定义实现 Runnable 接口(本质上也是实现了 Runnable)

    MyRunnable myRunnable = new MyRunnable();Thread thread = new Thread(myRunnable);thread.start();/*** 自定义类MyRunnable实现java.lang.Runnable接口*/class MyRunnable implements Runnable {@Overridepublic void run() {while (true) {SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");String dateStr = sdf.format(new Date());System.out.println("线程等待实现定时任务1:" + dateStr);try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}}}}

测试:
在这里插入图片描述

方法三: Timer: JDK 自带的定时任务类。 优点: 使用方便。

 //定义一个定时任务TimerTask timerTask = new TimerTask() {@Overridepublic void run() {SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");String dateStr = sdf.format(new Date());System.out.println("运行定时任务:" + dateStr);}};//计时器Timer timer = new Timer();//添加执行任务,延迟 delay 开始执行任务、 每 3s 执行一次// timer.schedule(timerTask, new Date(), 3000);// 添加执行任务,延迟 5s 开始执行 ,然后每隔一分钟执行一次Date date = new Date();//每分钟执行一次定时任务timer.scheduleAtFixedRate(timerTask,date, 60000);

测试:

在这里插入图片描述

举例: 每天的固定时间执行任务


```java/*** Thread 线程的等待*/public static void main(String[] args) {final int[] count = {1};TimerTask task = new TimerTask() {@Overridepublic void run() {//此处编写需要进行定时执行的任务;count[0] = count[0] + 1;System.out.println("时间=" + new Date() + " 执行了" + count[0] + "次"); // 1次}};//设置执行时间Calendar calendar = Calendar.getInstance();int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int day = calendar.get(Calendar.DAY_OF_MONTH);//每天//定制每天的23:00:00执行,calendar.set(year, month, day, 15, 17, 00);Date date = calendar.getTime();Timer timer = new Timer();System.out.println(date);//每天的date时刻执行task,每隔2秒重复执行int period = 2 * 1000;//timer.schedule(task, date, period);//每天的date时刻执行task, 仅执行一次timer.schedule(task, date);}}

方法4: Spring 的定时任务:注解式定时任务

写Spring 定时任务相关的demo

@Component
@Slf4j
public class SpringTaskDemo {@Scheduled(cron = "0/5 * * * * *")public void scheduled(){log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());}@Scheduled(fixedRate = 5000)public void scheduled1() {log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());}@Scheduled(fixedDelay = 5000)public void scheduled2() {log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis());}}

主类中开启定时任务的注解/ 在需要定时器的类上加注解

在启动类上加注解
需要定时的类上加注解
在这里插入图片描述

spring 定时任务参考博客:https://blog.csdn.net/weixin_44768683/article/details/125702724*
视频学习地址:https://www.bilibili.com/video/BV1xJ411G7ff

源码解析:https://mp.weixin.qq.com/s/CBAzqoSAG1QJhfZBEuPtlg

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...