Home  >  Article  >  Java  >  SpringBoot-scheduled tasks

SpringBoot-scheduled tasks

(*-*)浩
(*-*)浩forward
2019-10-16 16:39:232138browse

In the process of our actual development projects, we often need scheduled tasks to help us do some things, such as counting the number of newly registered users every hour, cleaning the server cache at 1 a.m. every day, and counting the number of purchased members every Friday. Number of users, etc.

SpringBoot-scheduled tasks

After version 2.0, SpringBoot provides a very convenient annotation method to write scheduled task programs without adding any configuration files and dependencies. You can quickly build a SpringBoot project by adding The annotations are as follows:

@SpringBootApplication
@EnableScheduling
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@EnableScheduling: Enable support for scheduled tasks

Annotation method - single thread

Create the SchedulerTask1 class and store it under the task package :

@Component
public class SchedulerTask1 {

    @Scheduled(cron = "*/6 * * * * ?")
    private void process(){
        System.out.println("SchedulerTask1 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
    }
}

Create the SchedulerTask2 class and store it under the task package:

@Component
public class SchedulerTask2 {
    @Scheduled(fixedRate  = 3000)
    private void process(){
        System.out.println("SchedulerTask2 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
    }
}

@Scheduled parameters can accept two timing settings, one is our commonly used cron="*/6 * * * * ?", one is fixedRate = 3000, both means printing content every X seconds.

fixedRate Description

@Scheduled(fixedRate = 3000): Execute 3 seconds after the last execution time point

@Scheduled(fixedDelay = 3000): Execute 3 seconds after the last execution completion time.

@Scheduled(initialDelay=1000, fixedRate=6000): Execute after a delay of 1 second for the first time, and then press

fixedRate The rule is executed every 6 seconds

Cron expression parameters respectively represent:

seconds (0~59) For example, 0/5 means every 5 seconds

Minutes (0~59)

Hours (0~23)

Day (0~31) On a certain day,

months (0~11) need to be calculated

Day of the week (can be filled in 1-7 or SUN/MON/TUE/WED/THU/FRI/SAT)

Start the project, the execution results are as follows:

SchedulerTask2 : 21:36:47.795008800
线程 : scheduling-1
SchedulerTask1 : 21:36:48.014888600
线程 : scheduling-1
SchedulerTask2 : 21:36:50.792887400
线程 : scheduling-1
SchedulerTask2 : 21:36:53.792697900
线程 : scheduling-1
SchedulerTask1 : 21:36:54.002684700
线程 : scheduling-1
SchedulerTask2 : 21:36:56.792517700
线程 : scheduling-1
SchedulerTask2 : 21:36:59.792606400
线程 : scheduling-1
SchedulerTask1 : 21:37:00.002598400
线程 : scheduling-1
SchedulerTask2 : 21:37:02.792423300
线程 : scheduling-1
SchedulerTask2 : 21:37:05.802238
线程 : scheduling-1
SchedulerTask1 : 21:37:06.002225200
线程 : scheduling-1

It can be seen that when the above two scheduled tasks are executed at the same time, the same thread is used: scheduling-1, and the execution time of the task will be affected by the execution time of the previous task.

Annotation method - multi-threading

Create a new MultithreadScheduleTask class and store it under the task package:

@Component
@EnableAsync
public class MultithreadScheduleTask {

    @Async
    @Scheduled(fixedDelay = 1000)
    public void first() throws InterruptedException {
        System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
        Thread.sleep(1000 * 10);
    }

    @Async
    @Scheduled(fixedDelay = 2000)
    public void second() {
        System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
        System.out.println();
    }

}

@EnableAsync: Enable multi-threading

Start the project and the running results are as follows:

第一个定时任务开始 : 21:48:47.121991300
线程 : task-1
第二个定时任务开始 : 21:48:47.121991300
线程 : task-2

第一个定时任务开始 : 21:48:48.121930600
线程 : task-3
第二个定时任务开始 : 21:48:49.114640
线程 : task-4

第一个定时任务开始 : 21:48:49.114640
线程 : task-5
第一个定时任务开始 : 21:48:50.119792400
线程 : task-6
第二个定时任务开始 : 21:48:51.119726900
线程 : task-7

第一个定时任务开始 : 21:48:51.129727800
线程 : task-8
第一个定时任务开始 : 21:48:52.130157200
线程 : task-2
第二个定时任务开始 : 21:48:53.120096500
线程 : task-4

第一个定时任务开始 : 21:48:53.140095
线程 : task-7
第一个定时任务开始 : 21:48:54.140429600
线程 : task-4
第二个定时任务开始 : 21:48:57.130710
线程 : task-1

The above is the detailed content of SpringBoot-scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete