SpringTask-Timer实现定时任务的详细代码
更新时间:2024年06月11日 10:24:22 作者:发飙的蜗牛'
在项目中开发定时任务应该一种比较常见的需求,今天通过示例代码给大家讲解SpringTask-Timer实现定时任务的相关知识,感兴趣的朋友一起看看吧
1、Timer 实现定时任务
1.1、JDK1.3 开始推出定时任务实现工具。
1.2、API

执行代码
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);
} 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进行整合

//pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>spring:
task:
execution:
thread-name-prefix: task_
shutdown:
await-termination: false
await-termination-period: 10s
scheduling:
pool:
size: 10 @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());
}到此这篇关于SpringTask-Timer实现定时任务的文章就介绍到这了,更多相关SpringTask定时任务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Mybatis plus的自动填充与乐观锁的实例详解(springboot)
这篇文章主要介绍了Mybatis plus的自动填充与乐观锁的实例详解(springboot),本文给大家介绍的非常详细对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-11-11
Springboot自动配置原理及DataSource的应用方式
这篇文章主要介绍了Springboot自动配置原理及DataSource的应用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-07-07
SpringBoot集成JWT生成token及校验方法过程解析
这篇文章主要介绍了SpringBoot集成JWT生成token及校验方法过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-04-04


最新评论