定时线程

说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务。
回顾一下定时线程池。

public static ScheduledExecutorService newScheduledThreadPool(int var0) {return new ScheduledThreadPoolExecutor(var0);}public static ScheduledExecutorService newScheduledThreadPool(int var0, ThreadFactory var1) {return new ScheduledThreadPoolExecutor(var0, var1);}

常用的两个方法:
scheduleAtFixedRate:是以固定的频率去执行任务,周期是指每次执行任务成功执行之间的间隔。

schedultWithFixedDelay:是以固定的延时去执行任务,延时是指上一次执行成功之后和下一次开始执行的之前的时间。

看一个DEMO:

public class ScheduledExecutorServiceDemo {public static void main(String args[]) {ScheduledExecutorService ses = Executors.newScheduledThreadPool(10);ses.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {try {Thread.sleep(4000);System.out.println(Thread.currentThread().getId() + "执行了");} catch (InterruptedException e) {e.printStackTrace();}}}, 0, 2, TimeUnit.SECONDS);}
}

具体细节我就不再赘述了,有兴趣的可以查看我关于线程池的博客:链接

springboot的定时任务

pom的依赖:

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

启动类启用定时

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling
@SpringBootApplication
public class StartApplication {public static void main(String args[]){SpringApplication application = new SpringApplication(StartApplication.class);application.run(args);}
}

定时任务业务类:

package com.schedule;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;@Component
public class ScheduleTask {private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");private AtomicInteger count = new AtomicInteger();@Scheduled(fixedRate = 6000)public void  reportTime(){System.out.println("现在的时间是:"+format.format(new Date()));}/*** 以固定的频率去执行任务*/@Scheduled(initialDelay = 10000,fixedRate = 3000)public void  reportNumber(){System.out.println(count.incrementAndGet());}/*** 以固定的延时去执行任务*/@Scheduled(initialDelay = 10000,fixedDelay = 3000)public void  reportNumberDelay(){System.out.println(count.incrementAndGet());}
}

运行结果如下:

现在的时间是:09:59:57
1
2
现在的时间是:10:00:03
3
4
5
6
现在的时间是:10:00:09
7

使用说明:

  • @Scheduled(fixedRate = 1000) :上一次开始执行时间点之后1秒再执行
  • @Scheduled(fixedDelay = 1000) :上一次执行完毕时间点之后1秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
    @Scheduled(initialDelay=1000, fixedDelay=6000) :第一次延迟1秒后执行,之后按fixedDelay的规则每6秒执行一次

源码地址

个人博客网站 http://www.janti.cn

springboot之定时任务相关推荐

  1. SpringBoot 实战定时任务 Scheduled

    序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

  2. springboot配置定时任务及常用的cron表达式

    springboot引入定时任务 springboot引入定时任务主要需要以下几步: 1.引入相关的依赖 2.配置程序开启定时任务 3. 编写定时任务 引入相关的依赖 只用引一个基础的web的依赖就可 ...

  3. SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊)

    SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 目录 SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 1.概述 2.最佳实践 2.1创建项目引入依赖(mail) ...

  4. SpringBoot实现定时任务的三种方式,总有一款适合你!

    点击关注公众号,利用碎片时间学习 序言 SpringBoot创建定时任务,目前主要有以下三种实现方式: 基于注解(@Scheduled): 基于注解@Scheduled默认为单线程,开启多个任务时,任 ...

  5. SpringBoot整合定时任务和Emil发送

    SpringBoot整合定时任务和Emil发送 定时任务 ​ 任务系统指的是定时任务.定时任务是企业级开发中必不可少的组成部分,诸如长周期业务数据的计算,例如年度报表,诸如系统脏数据的处理,再比如系统 ...

  6. linux定时任务重复率,基于SpringBoot实现定时任务的设置(常用:定时清理数据库)...

    1.构建SpringBoot工程项目 1)创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @Ena ...

  7. SpringBoot | :定时任务的使用

    前言 上一章我们简单的讲解了关于异步请求相关知识点.这一章节,我们来讲讲开发过程也是经常会碰见的定时任务.比如每天定时清理无效数据.定时发送短信.定时发送邮件.支付系统中的定时对账等等,往往都会定义一 ...

  8. SpringBoot中定时任务与异步定时任务的实现

    场景 若依前后端分离版手把手教你本地搭建环境并运行项目: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662 在上面 ...

  9. 复盘SpringBoot中定时任务和异步线程池

    作者:溪~源 blog.csdn.net/xuan_lu/article/details/110568508 项目中最近使用了多个定时任务处理业务需求,于是在实现业务逻辑过程中,产生了上图一些思考和疑 ...

最新文章

  1. 26期20180601目录管理
  2. Citrix XenDesktop 7.X 视频播放优化
  3. 我的Java开发学习之旅------Base64的编码思想以及Java实现
  4. ffmpeg开发指南(使用 libavformat 和 libavcodec)
  5. 复杂网络环境下的访问控制技术
  6. Android Studio 1.1.0汉化初步出炉!
  7. oracle 能被2整除_2021辽宁公务员考试:好用的“整除”法
  8. Hadoop 系列 HDFS:分布式文件系统(HDFS文件读写)
  9. kali linux 安装中文输入法
  10. Visual Studio 2015离线版msdn帮助文档下载和安装
  11. 大数据采集与预处理技术
  12. dockerfile
  13. 惠群计算机科技,电脑报专访:探索新视角,再造多元化的宏碁
  14. HTTP错误代码 404 503 500
  15. 讲座录播|图数据库中的子图匹配算法-邹磊
  16. 小区宽带不能上网的解决办法
  17. rt2870 linux,『求助』RaLink雷凌RT2870 无线网卡怎样安装驱动?
  18. for循环和while循环哪个效率更高
  19. React.js读书与总结:《react-tutorial》
  20. 这届中国球迷多任性?盘点2018世界杯球迷画像

热门文章

  1. 【Qt】错误GL/gl.h: No such file or directory的解决方法(以及cannot find -lGL解决方法)
  2. 谁的青春不迷茫,其实我们都一样
  3. android 访问http地址吗,浅谈android访问http原理
  4. php ziparchive 压缩文件,php使用ZipArchive压缩打包文件
  5. solidworks画白色金属光泽_美人的共通点就是卧蚕,卧蚕真的太重要了,没有也要画出来...
  6. Java项目:财务预算管理系统(java+SSM+Jsp+Mysql+Layui+Maven)
  7. aop point 只能获取到map嘛_面试被问了几百遍的 IoC 和 AOP ,还在傻傻搞不清楚?...
  8. 在文件中查找指定字符串
  9. PHP中阶,PHP进阶
  10. 样式集(9) - 切换Tab菜单