👉 这是一个或许对你有用的社群
🐱 一对一交流/面试小册/简历优化/求职解惑,欢迎加入「芋道快速开发平台」知识星球。下面是星球提供的部分资料:

👉这是一个或许对你有用的开源项目
国产Star破10w的开源项目,前端包括管理后台、微信小程序,后端支持单体、微服务架构
RBAC权限、数据权限、SaaS多租户、商城、支付、工作流、大屏报表、ERP、CRM、AI大模型、IoT物联网等功能:
多模块:https://gitee.com/zhijiantianya/ruoyi-vue-pro 微服务:https://gitee.com/zhijiantianya/yudao-cloud 视频教程:https://doc.iocoder.cn 【国内首批】支持 JDK17/21+SpringBoot3、JDK8/11+Spring Boot2双版本
来源:juejin.cn/post/7496369162485317647
想象你有个24小时待命的英国管家:
这就是Spring Task的本质——让程序学会自己"定闹钟"!相比传统的Timer,它就像从诺基亚升级到iPhone:

基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://github.com/YunaiV/ruoyi-vue-pro 视频教程:https://doc.iocoder.cn/video/
<!-- 使用前先安装"发条" --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency>Spring Boot 2.x+版本已经内置定时任务模块,无需额外添加依赖
@SpringBootApplication@EnableScheduling// 给程序装上定时芯片publicclassTaskApplication{publicstaticvoidmain(String[] args){ SpringApplication.run(TaskApplication.class, args); }}@ComponentpublicclassMyTask{// 每天23:59:59执行(打工人日报提醒)@Scheduled(cron = "59 59 23 * * ?")publicvoiddailyReport(){ System.out.println("【系统提示】记得写日报!"); }}基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://github.com/YunaiV/yudao-cloud 视频教程:https://doc.iocoder.cn/video/
秒 分 时 日 月 周 年(可选)记忆口诀: "秒杀时分日月周年"

*:比老板的要求更野(每时每刻)?:佛系青年专用(不指定具体值)L:最后的倔强(最后一天)W:社畜必修(最近工作日)#:霸道总裁选择(第N个周X)@Scheduled(fixedRate = 3600000) // 每小时跑腿一次publicvoidsyncOrderStatus(){// 把订单系统的状态"搬运"到物流系统}@Scheduled(cron = "0 0 3 * * ?") // 每天凌晨3点打扫publicvoidcleanLogs(){// 把7天前的日志文件"扫"进回收站}@Scheduled(cron = "0 0 9 ? * MON") // 每周一早上9点publicvoidsendWeeklyReport(){// 自动给老板发送"表面功夫"周报}@Scheduled(fixedDelay = 5000) // 5秒后重复@Scheduled(fixedRate = 3000) // 3秒一次@Scheduled(initialDelay = 10000, fixedRate = 5000) // 10秒后开始,每5秒一次@ConfigurationpublicclassTaskConfigimplementsSchedulingConfigurer{@OverridepublicvoidconfigureTasks(ScheduledTaskRegistrar taskRegistrar){// 创建10个线程的定时任务线程池 taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10)); }}当多个实例同时运行时:
默认单线程执行时,前一个任务卡住会导致后续任务排队。解决方案:
@EnableAsync// 开启异步模式@Async// 给方法加上"加速器"@Scheduled(fixedRate = 1000)publicvoidasyncTask(){// 现在不会堵车了}使用fixedDelay代替fixedRate:
@Scheduled(fixedDelay = 5000) // 每次执行结束后等5秒0 */5 * * * ? 每5分钟(整点开始)0 5/10 * * * ? 每小时的第5分钟开始,每10分钟一次0 0 12 1W * ? 每月最接近1号的工作日中午12点@Around("@annotation(scheduled)")public Object monitor(ProceedingJoinPoint pjp, Scheduled scheduled)throws Throwable {long start = System.currentTimeMillis();try {return pjp.proceed(); } finally {long cost = System.currentTimeMillis() - start; log.info("任务执行耗时:{}ms", cost); }}# application.propertiesschedule.enabled=true@ConditionalOnProperty(name = "schedule.enabled", havingValue = "true")@Scheduled(cron = "${schedule.cron}")publicvoidconfigurableTask(){// 可配置的任务}// 动态任务示例@Autowiredprivate ScheduledTaskRegistrar taskRegistrar;publicvoidaddDynamicTask(Runnable task, String cron){ taskRegistrar.addCronTask(new CronTask(task, cron));}定时任务虽好,但不要贪杯哦!
当你的任务开始需要以下功能时,就该考虑专业调度框架了:
现在就去给你的程序装上"定时芯片"吧!如果遇到任何问题,欢迎在评论区呼叫"任务救援队"~
欢迎加入我的知识星球,全面提升技术能力。
👉 加入方式,“长按”或“扫描”下方二维码噢:

星球的内容包括:项目实战、面试招聘、源码解析、学习路线。





文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)