前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot之定时任务

springboot之定时任务

作者头像
Janti
发布2018-08-01 11:25:04
3160
发布2018-08-01 11:25:04
举报
文章被收录于专栏:JantiJanti

定时线程

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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的依赖:

代码语言:javascript
复制
    <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>

启动类启用定时

代码语言:javascript
复制
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);
    }
}

定时任务业务类:

代码语言:javascript
复制
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());
    }
}

运行结果如下:

代码语言:javascript
复制
现在的时间是: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秒执行一次

源码地址

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 定时线程
  • springboot的定时任务
    • pom的依赖:
      • 启动类启用定时
        • 定时任务业务类:
          • 使用说明:
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档