springBoot @Scheduled实现多个任务同时开始执行
作者:李振伟 发布时间:2023-06-19 23:16:41
标签:springBoot,@Scheduled,多任务,执行
@Scheduled多个任务同时开始执行
只需在springBoot启动类上添加
如下代码即可:
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskExecutor = new ThreadPoolTaskScheduler();
taskExecutor.setPoolSize(50);
return taskExecutor;
}
@Scheduled多定时任务,重叠执行
@Scheduled如果有两个定时任务
定时任务重复时,只有一个可以执行。
如下
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
public class MyScheduled {
@Scheduled(cron = "0/5 * * * * ?")
public void execute1(){
String curName = Thread.currentThread().getName() ;
System.out.println("当前时间:"+LocalDateTime.now()+" 任务execute1对应的线程名: "+curName);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
@Scheduled(cron = "0/5 * * * * ?")
public void execute2(){
String curName = Thread.currentThread().getName() ;
System.out.println("当前时间:"+LocalDateTime.now()+" 任务execute2对应的线程名: "+curName);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
通过执行可以看到,打印线程名称为同一个。即如果不手动指定线程池,则默认启动单线程,进行执行定时任务。
如果想要多个定时任务重叠执行
需要手动指定线程池,如下
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@EnableScheduling
public class MyScheduled {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(50);
return taskScheduler;
}
@Scheduled(cron = "0/5 * * * * ?")
public void execute1(){
String curName = Thread.currentThread().getName() ;
System.out.println("当前时间:"+LocalDateTime.now()+" 任务execute1对应的线程名: "+curName);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
@Scheduled(cron = "0/5 * * * * ?")
public void execute2(){
String curName = Thread.currentThread().getName() ;
System.out.println("当前时间:"+LocalDateTime.now()+" 任务execute2对应的线程名: "+curName);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
此时,多个定时任务,是不通的线程执行,同时,定时任务可以重叠执行。
来源:https://blog.csdn.net/lifulian318/article/details/114277044


猜你喜欢
- 一、Surface 概述OpenGL ES/Skia定义了一组绘制接口的规范,为什么能够跨平台? 本质上需要与对应平台上的本地窗口建立连接。
- C/C++的数据类型:一,整型Turbo C: [signed] int 2Byte//有符号数,-32768~32
- 网上查找资料要么是细节不够失败要么是根本没用也不需要这么复杂,在这里总结一下本人在宝塔部署前端和后端的方法。1.在宝塔上添加站点&u
- 前言最近VS2019正式版发布了,装下来顺便试用了一下C#8.0,最大的看点应该就是可空引用类型了。不过C#8.0仍然处于Beta的状态,而
- 今天,写了个小代码。抓取首页中的极客头条。效果如图:分享给新手朋友。要点:1.使用ApacheHttpClient库实现GET请求。2.异步
- 比如定义了一个错误的枚举类型public enum eErrorDetailCode : int &nbs
- 全面总结Android Service的使用方法,具体内容如下1、Service的种类按运行地点分类:其实remote服务还是很少见的,并且
- 我们在应用中经常看到一些选择开关状态的配置文件,做项目的时候用的是android的Switch控件,但是感觉好丑的样子子个人认为还是自定义的
- 一、MessageBox弹出框MessageBox.Show(<字符串> Text, <字符串> Title, &l
- Android Studio在实现隐藏标题栏和状态栏上和Eclipse是完全不一样的。在Eclipse上隐藏标题栏和状态栏的代码如下:方法一
- springboot使用mybatis一对多的关联查询由于刚开始写java不久,对sql语句的熟悉度还是不够熟练,虽然现在使用的mybati
- 本系列代码地址:https://github.com/JoJoTec/spring-cloud-parentOpenFeign 的由来和实现
- Timer 详解Timer 和 TimerTask 用于在后台线程中调度任务的 java.
- 目录前言if-thenif-then-elseswitch使用 Stringwhiledo-whileforbreakcontinueret
- 题外话: 泛型与通配符是Java语法中比较难懂的两个语法,学习泛型和通配符的主要目的是能够看懂源码,实际使用的不多。1.泛型1.1泛型的用法
- 背景知识Fluent Interface是一种通过连续的方法调用以完成特定逻辑处理的API实现方式,在代码中引入Fluent Interfa
- 本文实例为大家分享了PopupWindow+RecyclerView实现上下滑动框功能的具体代码,供大家参考,具体内容如下1.新建一个适配器
- 本文实例讲述了Android中使用Service实现后台发送邮件功能。分享给大家供大家参考,具体如下:程序如下:import android
- 由于我们在eclipse ee中把项目部署在web端经常会出现报404错误。原因为:404状态码是一种http状态码,其意思是: 所请求的页
- 昨天遇到了点问题解决浪费了一些时间(导致更新内容较少)回顾下问题项目出现Unable to import maven project: Se