There are two types of scheduled tasks:

  1. Based on the annotation
  2. Based on the interface

Based on comments @Scheduled

@service public class Scheduling1Service {// Scheduling1Service is executed every 2 seconds. @scheduled (fixedRate = 2000) public void test1() throws InterruptedException { Thread.sleep(1000L); Thread. Sleep (3000L); Date Date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(format.format(date)+"==>SchedulingService.test1 is called"); } // 2 seconds after the last task finished, @scheduled (fixedDelay = 2000) public void test2() throws InterruptedException {thread.sleep (3000L); Date Date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(format.format(date)+"==>SchedulingService.test2 is called"); } // Support for corn expressions @scheduled (cron = "0 0 1 * *?") ) public void test3() throws InterruptedException {Date Date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(format.format(date)+"==>SchedulingService.test3 is called"); }}

Note: Cannot write
cornFor expressions, you can use this:
https://cron.qqe2.comCorn expressions are generated automatically and can check if your expressions are valid. Very easy to use!

The above three are used more frequently. This scenario is a service scenario in which users periodically synchronize basic data from a third party because parameters are not accepted.

use
@ScheduledSpringboot dependencies need to be referenced in the POM and added in the Application main entry function
@EnableSchedulingAnnotations.

Schedule tasks based on interfaces

The annotation-based approach to task configuration is simple and useful, but its use is limited by the inability to pass parameters. Then you need to use timing tasks in the form of interfaces.

Add a dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
@Service public class Scheduling3Service implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.addTriggerTask( new Runnable() { @Override public void run() { System.out.println("cccccccc"); } }, triggerContext -> { return new CronTrigger("0/1 * * * * ? ").nextExecutionTime(triggerContext); }); }}

These are two commonly used timing tasks, friends, you, learn waste?

More Java original reading: https://javawu.com