SpringTask-Timer实现定时任务

1、Timer 实现定时任务

1.1、JDK1.3 开始推出定时任务实现工具。

1.2、API

执行代码

javascript 复制代码
    public static void main(String[] args) throws ParseException {
        Timer timer = new Timer();
        String str="2024-06-10 23:24:00";
        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("定时任务执行");
                System.out.println("定时任务执行时间--》"+new Date());
            }
        },date);
    }
javascript 复制代码
    public static void main(String[] args) throws ParseException {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("定时任务执行");
                System.out.println("定时任务执行时间--》"+new Date());
            }
        },0,2000);
    }

2、使用spring进行整合

javascript 复制代码
//pom文件
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
javascript 复制代码
spring:
  task:
    execution:
      thread-name-prefix: task_
      shutdown:
        await-termination: false
        await-termination-period: 10s
    scheduling:
      pool:
        size: 10
javascript 复制代码
    @Scheduled(cron = "0/3 * * * * ? ")
    public void test1() {
        System.out.println("定时任务执行test1");
        System.out.println("定时任务执行时间--》"+new Date());
    }
    @Scheduled(cron = "0/3 * * * * ? ")
    public void test2() {
        System.out.println("定时任务执行test2");
        System.out.println("定时任务执行时间--》"+new Date());
    }
相关推荐
枫叶丹419 小时前
【Qt开发】Qt系统(一)-> 定时器 QTimerEvent 和 QTimer
c语言·开发语言·数据库·c++·qt·系统架构
鸽鸽程序猿19 小时前
【JavaEE】【SpringCloud】环境与工程搭建
java·spring cloud·java-ee
我居然是兔子1 天前
异常练习:在试错中吃透Java异常处理的底层逻辑
java·开发语言
养一回月亮!1 天前
使用Qt实现简单绘图板:鼠标绘制与擦除功能详解
开发语言·qt
BanyeBirth1 天前
C++差分数组(二维)
开发语言·c++·算法
Tony Bai1 天前
Go 的 AI 时代宣言:我们如何用“老”原则,解决“新”问题?
开发语言·人工智能·后端·golang
Fcy6481 天前
C++ map和multimap的使用
开发语言·c++·stl
CC.GG1 天前
【C++】STL容器----unordered_map和unordered_set的使用
java·数据库·c++
L Jiawen1 天前
【Golang基础】基础知识(下)
服务器·开发语言·golang
Overt0p1 天前
抽奖系统(4)
java·spring boot·tomcat