SpringBoot timer timing scheduling

how to use

SpringBoot itself has provided us with a built-in timer Scheduled, which we can use directly.

@Configuration
@EnableScheduling
public class SchedulingConfig {

    @Scheduled(cron = "0 0/1 * * * ?")
    public void test(){
        System.out.println("定时调度。。。。。。。。。。。。");
    }
}

@Sheduled annotation

Cron attribute - String

Seconds (seconds) : four characters ", - * /" can appear, and the valid range is an integer from 0-59
Minutes (minutes) : four characters ", - * /" can appear, and the valid range is an integer from 0-59
Hours (hour) : can appear ", - * /" four characters, the valid range is an integer from 0-23
DayofMonth (day/month) : can appear ", - * / ? LW C" eight characters, the valid range is Integer of 0-31
Month (month) : four characters ", - * /" can appear, and the valid range is an integer from 1-12 or JAN-DEc
DayofWeek (day of the week) : ", - * / ? LC # can appear "Four characters, an integer with a valid range of 1-7 or two ranges of SUN-SAT. 1 means Sunday, 2 means Monday, and so on
Year (year) : four characters ", - * /" can appear, and the valid range is 1970-2099
Recommend a website automatically generated by cron expression  click to get

fixedRate attribute - Long

Indicates how long to execute once, the unit is ms (milliseconds), and then execute

fixedDelay property - Long

Indicates how long to execute once, and the unit is ms (milliseconds). The difference between executing fixedRate and fixedDelay first is that the timer fixedRate is executed first and then the code block is executed, while fixedDelay executes the code block first, and then executes the timer. The following code is used to demonstrate

@Configuration
@EnableScheduling
public class SchedulingConfig {

    @Scheduled(fixedRate = 10000)
    public void test1() throws InterruptedException {
        Thread.sleep(5000);
        System.out.println("定时任务1================");
    }

    @Scheduled(fixedRate = 10000)
    public void test2() throws InterruptedException {
        Thread.sleep(5000);
        System.out.println("定时任务2================");
    }
}

Start the application, the output is

定时任务2================
定时任务1================
定时任务2================
定时任务1================

initialDelay attribute - Long

This attribute indicates the delay time of the first execution, which is only valid for the first execution

Guess you like

Origin blog.csdn.net/qq_21046665/article/details/79684226