博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot之定时任务
阅读量:7037 次
发布时间:2019-06-28

本文共 3130 字,大约阅读时间需要 10 分钟。

定时线程

说到定时任务,通常会想到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() {            @Override            public void run() {                try {                    Thread.sleep(4000);                    System.out.println(Thread.currentThread().getId() + "执行了");                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }, 0, 2, TimeUnit.SECONDS);    }}

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

springboot的定时任务

pom的依赖:

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test

启动类启用定时

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling@SpringBootApplicationpublic 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;@Componentpublic 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:5712现在的时间是:10:00:033456现在的时间是:10:00:097

使用说明:

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

转载于:https://www.cnblogs.com/superfj/p/8989277.html

你可能感兴趣的文章
围住神经猫,朋友圈瞬间爆红是如何炼成的?
查看>>
HDUoj-------(1128)Self Numbers
查看>>
huffman编码——原理与实现
查看>>
php curl获取网页内容乱码和获取不到内容的解决方法
查看>>
28、activity之间传递数据&批量传递数据
查看>>
混沌数学之Rössler(若斯叻)吸引子
查看>>
【JavaScript】关于prototype
查看>>
普通Jquery的ajax判断重复和formvalidator的ajaxValidator区别
查看>>
ovs处理openflow消息的流程
查看>>
精品素材:WALK & RIDE 单页网站模板下载
查看>>
大数运算
查看>>
Android开发学习笔记-SharedPreferences的用法
查看>>
Thread message loop for a thread with a hidden window? Make AllocateHwnd safe
查看>>
几家SIEM
查看>>
25个超简约风格的国外酷站设计案例
查看>>
宁做创业狼,不做打工狗
查看>>
今儿开通博客咯...嘎嘎
查看>>
java在linux上始终无法用jdbc跟myql连接
查看>>
Atitit.故障排除系列-----apache 不能启动的排除
查看>>
怎样对ListView的项进行排序
查看>>