Spring Boot + @EnableScheduling + @Scheduled + Scheduled Tasks

Spring boot provides excellent features for both task scheduling and asynchronous method execution based on cron expression using @Scheduled annotation. The @Scheduled annotation can be added to a method along with trigger metadata.

In this example, we will learn how to use @Scheduled annotation in 4 different ways.

Technology Stack

Java 8
Maven 3.5.0
Spring Boot 2.4.4
Red Hat JBoss Developer Studio 11.3.0.GA / Eclipse

Project Structure

High level project structure would be like this.

Project Structure

Spring Boot @EnableScheduling and @Scheduled Annotations

@EnableScheduling and @Scheduled annotations need to be added for Scheduling tasks in Spring Boot Application. 
@EnableScheduling should be applied to the SpringBootApplication.

package com.the.basic.tech.info.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SchedulerDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SchedulerDemoApplication.class, args);
	}
}

@Scheduled must be applied on any method but that method should not take any arguments and should not return any value hence void should be return type.

package com.the.basic.tech.info.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;


@Component
public class ScheduledTasks {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");  
    @Scheduled(cron = "0 * * * * ?")
    public void scheduleTaskWithCronExpression() {
        logger.info("Cron Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
    }
}

Maven File (pom.xml)

pom.xml mavel file contains all the maven dependency details.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>spring-boot-scheduler</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-scheduler</name>
	<description>Spring Boot microservice project for scheduler</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.4</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-runner</artifactId>			
			<scope>test</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

SchedulerConfig

SchedulingConfigurer functional interface needs to be implement for overriding the configureTasks method.

package com.the.basic.tech.info.app.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;


@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
    private final int POOL_SIZE = 10;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
        threadPoolTaskScheduler.setThreadNamePrefix("my-scheduled-task-pool-");
        threadPoolTaskScheduler.initialize();

        scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
    }
}

Example: Scheduled Task with Cron Expression

cron is a feature originating from Unix cron utility and has various options based on your requirements.

package com.the.basic.tech.info.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;



@Component
public class ScheduledTasks {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");  
    @Scheduled(cron = "0 * * * * ?")
    public void scheduleTaskWithCronExpression() {
        logger.info("Cron Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
    }

}


Output

2021-05-28 18:11:54.526  INFO 3776 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : No active profile set, falling back to default profiles: default
2021-05-28 18:11:56.283  INFO 3776 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2021-05-28 18:11:56.309  INFO 3776 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : Started SchedulerDemoApplication in 3.04 seconds (JVM running for 4.047)
2021-05-28 18:12:00.011  INFO 3776 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Cron Task :: Execution Time - 18:12:00
2021-05-28 18:13:00.002  INFO 3776 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Cron Task :: Execution Time - 18:13:00

Example: Scheduled Task with fixedRate

fixedRate makes Spring boot run the task on periodic intervals even if the last invocation may be still running.

package com.the.basic.tech.info.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;


@Component
public class ScheduledTasks {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Scheduled(fixedRate = 2000)
    public void scheduleTaskWithFixedRate() {
        logger.info("Fixed Rate Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()) );
    }

}

Output
2021-05-28 18:17:41.070  INFO 16588 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : No active profile set, falling back to default profiles: default
2021-05-28 18:17:41.926  INFO 16588 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2021-05-28 18:17:41.926  INFO 16588 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:41
2021-05-28 18:17:41.941  INFO 16588 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : Started SchedulerDemoApplication in 1.406 seconds (JVM running for 1.979)
2021-05-28 18:17:43.945  INFO 16588 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:43
2021-05-28 18:17:45.976  INFO 16588 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:45
2021-05-28 18:17:47.955  INFO 16588 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:47
2021-05-28 18:17:49.939  INFO 16588 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:49
2021-05-28 18:17:51.939  INFO 16588 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:51
2021-05-28 18:17:53.939  INFO 16588 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:53
2021-05-28 18:17:55.939  INFO 16588 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:55
2021-05-28 18:17:57.940  INFO 16588 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:57
2021-05-28 18:17:59.940  INFO 16588 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:17:59
2021-05-28 18:18:01.941  INFO 16588 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:18:01
2021-05-28 18:18:03.941  INFO 16588 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:18:03
2021-05-28 18:18:05.940  INFO 16588 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:18:05

Example: Scheduled Task with fixedDelay

fixedDelay specifically controls the next execution time when the last execution finishes.

package com.the.basic.tech.info.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;


@Component
public class ScheduledTasks {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Scheduled(fixedDelay = 2000)
    public void scheduleTaskWithFixedDelay() {
        logger.info("Fixed Delay Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException ex) {
            logger.error("Ran into an error {}", ex);
            throw new IllegalStateException(ex);
        }
    }

}


Output

2021-05-28 18:24:39.624  INFO 6704 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : No active profile set, falling back to default profiles: default
2021-05-28 18:24:40.516  INFO 6704 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2021-05-28 18:24:40.524  INFO 6704 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:24:40
2021-05-28 18:24:40.533  INFO 6704 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : Started SchedulerDemoApplication in 1.725 seconds (JVM running for 2.671)
2021-05-28 18:24:43.528  INFO 6704 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:24:43
2021-05-28 18:24:46.529  INFO 6704 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:24:46
2021-05-28 18:24:49.533  INFO 6704 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:24:49
2021-05-28 18:24:52.535  INFO 6704 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:24:52
2021-05-28 18:24:55.551  INFO 6704 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:24:55
2021-05-28 18:24:58.559  INFO 6704 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:24:58
2021-05-28 18:25:01.564  INFO 6704 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:25:01
2021-05-28 18:25:04.568  INFO 6704 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:25:04
2021-05-28 18:25:07.571  INFO 6704 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:25:07
2021-05-28 18:25:10.574  INFO 6704 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:25:10
2021-05-28 18:25:13.580  INFO 6704 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:25:13
2021-05-28 18:25:16.585  INFO 6704 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:25:16
2021-05-28 18:25:19.588  INFO 6704 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:25:19
2021-05-28 18:25:22.590  INFO 6704 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:25:22

Example: Scheduled Task with fixedRate and initialDelay

package com.the.basic.tech.info.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;


@Component
public class ScheduledTasks {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");   

    @Scheduled(fixedRate = 2000, initialDelay = 5000)
    public void scheduleTaskWithInitialDelay() {
        logger.info("Fixed Rate Task with Initial Delay :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
    }
}


Output
2021-05-28 18:31:01.937  INFO 9520 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : No active profile set, falling back to default profiles: default
2021-05-28 18:31:03.024  INFO 9520 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2021-05-28 18:31:03.040  INFO 9520 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : Started SchedulerDemoApplication in 1.919 seconds (JVM running for 2.866)
2021-05-28 18:31:08.033  INFO 9520 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:08
2021-05-28 18:31:10.032  INFO 9520 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:10
2021-05-28 18:31:12.034  INFO 9520 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:12
2021-05-28 18:31:14.037  INFO 9520 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:14
2021-05-28 18:31:16.033  INFO 9520 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:16
2021-05-28 18:31:18.044  INFO 9520 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:18
2021-05-28 18:31:20.034  INFO 9520 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:20
2021-05-28 18:31:22.033  INFO 9520 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:22
2021-05-28 18:31:24.039  INFO 9520 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:24
2021-05-28 18:31:26.047  INFO 9520 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:26
2021-05-28 18:31:28.033  INFO 9520 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:28
2021-05-28 18:31:30.034  INFO 9520 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:30
2021-05-28 18:31:32.039  INFO 9520 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:32
2021-05-28 18:31:34.033  INFO 9520 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:34
2021-05-28 18:31:36.035  INFO 9520 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:36
2021-05-28 18:31:38.035  INFO 9520 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:38
2021-05-28 18:31:40.033  INFO 9520 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:31:40

Example: Scheduled Tasks with all 4 ways

package com.the.basic.tech.info.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@Component
public class ScheduledTasks {

	private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

	private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

	@Scheduled(fixedRate = 2000)
	public void scheduleTaskWithFixedRate() {
		logger.info("Fixed Rate Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
	}

	@Scheduled(fixedDelay = 2000)
	public void scheduleTaskWithFixedDelay() {
		logger.info("Fixed Delay Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException ex) {
			logger.error("Ran into an error {}", ex);
			throw new IllegalStateException(ex);
		}
	}

	@Scheduled(fixedRate = 2000, initialDelay = 5000)
	public void scheduleTaskWithInitialDelay() {
		logger.info("Fixed Rate Task with Initial Delay :: Execution Time - {}",
				dateTimeFormatter.format(LocalDateTime.now()));
	}

	@Scheduled(cron = "0 * * * * ?")
	public void scheduleTaskWithCronExpression() {
		logger.info("Cron Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
	}

}

Output
2021-05-28 18:34:18.005  INFO 17928 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : No active profile set, falling back to default profiles: default
2021-05-28 18:34:19.147  INFO 17928 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2021-05-28 18:34:19.158  INFO 17928 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:34:19
2021-05-28 18:34:19.162  INFO 17928 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:34:19
2021-05-28 18:34:19.167  INFO 17928 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : Started SchedulerDemoApplication in 1.94 seconds (JVM running for 2.816)
2021-05-28 18:34:21.160  INFO 17928 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:34:21
2021-05-28 18:34:22.165  INFO 17928 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:34:22
2021-05-28 18:34:23.159  INFO 17928 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:34:23
2021-05-28 18:34:24.158  INFO 17928 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:34:24
2021-05-28 18:34:25.160  INFO 17928 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:34:25
2021-05-28 18:34:25.176  INFO 17928 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:34:25
2021-05-28 18:34:26.162  INFO 17928 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:34:26
2021-05-28 18:34:27.164  INFO 17928 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:34:27
2021-05-28 18:34:28.158  INFO 17928 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:34:28
2021-05-28 18:34:28.180  INFO 17928 --- [led-task-pool-8] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:34:28
2021-05-28 18:34:29.166  INFO 17928 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:34:29
2021-05-28 18:34:30.157  INFO 17928 --- [led-task-pool-9] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:34:30
2021-05-28 18:34:31.169  INFO 17928 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:34:31
2021-05-28 18:34:31.185  INFO 17928 --- [ed-task-pool-10] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:34:31
2021-05-28 18:34:32.172  INFO 17928 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:34:32

Example: Running Parallel Tasks (@Async)

If your tasks that can be run parallely and next job not needed output of the previous run then you can use @EnableAync power in Spring Boot.

package com.the.basic.tech.info.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@Component
public class ScheduledTasks {

	private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

	private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

	@Async	
	@Scheduled(fixedRate = 2000)
	public void scheduleTaskWithFixedRate() {
		logger.info("Fixed Rate Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
	}

	@Async
	@Scheduled(fixedDelay = 2000)
	public void scheduleTaskWithFixedDelay() {
		logger.info("Fixed Delay Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException ex) {
			logger.error("Ran into an error {}", ex);
			throw new IllegalStateException(ex);
		}
	}

	@Async
	@Scheduled(fixedRate = 2000, initialDelay = 5000)
	public void scheduleTaskWithInitialDelay() {
		logger.info("Fixed Rate Task with Initial Delay :: Execution Time - {}",
				dateTimeFormatter.format(LocalDateTime.now()));
	}

	@Async
	@Scheduled(cron = "0 * * * * ?")
	public void scheduleTaskWithCronExpression() {
		logger.info("Cron Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
	}

}

Output
2021-05-28 18:53:31.270  INFO 12772 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : No active profile set, falling back to default profiles: default
2021-05-28 18:53:32.376  INFO 12772 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2021-05-28 18:53:32.386  INFO 12772 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:32
2021-05-28 18:53:32.386  INFO 12772 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:32
2021-05-28 18:53:32.393  INFO 12772 --- [           main] c.t.b.t.i.app.SchedulerDemoApplication   : Started SchedulerDemoApplication in 1.785 seconds (JVM running for 2.513)
2021-05-28 18:53:34.386  INFO 12772 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:34
2021-05-28 18:53:35.390  INFO 12772 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:35
2021-05-28 18:53:36.386  INFO 12772 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:36
2021-05-28 18:53:37.387  INFO 12772 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:37
2021-05-28 18:53:38.386  INFO 12772 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:38
2021-05-28 18:53:38.391  INFO 12772 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:38
2021-05-28 18:53:39.387  INFO 12772 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:39
2021-05-28 18:53:40.388  INFO 12772 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:40
2021-05-28 18:53:41.389  INFO 12772 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:41
2021-05-28 18:53:41.396  INFO 12772 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:41
2021-05-28 18:53:42.388  INFO 12772 --- [led-task-pool-8] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:42
2021-05-28 18:53:43.388  INFO 12772 --- [led-task-pool-9] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:43
2021-05-28 18:53:44.386  INFO 12772 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:44
2021-05-28 18:53:44.398  INFO 12772 --- [ed-task-pool-10] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:44
2021-05-28 18:53:45.387  INFO 12772 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:45
2021-05-28 18:53:46.386  INFO 12772 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:46
2021-05-28 18:53:47.387  INFO 12772 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:47
2021-05-28 18:53:47.400  INFO 12772 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:47
2021-05-28 18:53:48.386  INFO 12772 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:48
2021-05-28 18:53:49.388  INFO 12772 --- [led-task-pool-8] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:49
2021-05-28 18:53:50.388  INFO 12772 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:50
2021-05-28 18:53:50.402  INFO 12772 --- [led-task-pool-9] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:50
2021-05-28 18:53:51.387  INFO 12772 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:51
2021-05-28 18:53:52.387  INFO 12772 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:52
2021-05-28 18:53:53.388  INFO 12772 --- [ed-task-pool-10] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:53
2021-05-28 18:53:53.402  INFO 12772 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:53
2021-05-28 18:53:54.387  INFO 12772 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:54
2021-05-28 18:53:55.388  INFO 12772 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:55
2021-05-28 18:53:56.386  INFO 12772 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:56
2021-05-28 18:53:56.405  INFO 12772 --- [led-task-pool-8] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:56
2021-05-28 18:53:57.387  INFO 12772 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:57
2021-05-28 18:53:58.388  INFO 12772 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:53:58
2021-05-28 18:53:59.387  INFO 12772 --- [led-task-pool-9] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:53:59
2021-05-28 18:53:59.407  INFO 12772 --- [led-task-pool-3] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:53:59
2021-05-28 18:54:00.007  INFO 12772 --- [ed-task-pool-10] c.t.basic.tech.info.app.ScheduledTasks   : Cron Task :: Execution Time - 18:54:00
2021-05-28 18:54:00.388  INFO 12772 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:54:00
2021-05-28 18:54:01.387  INFO 12772 --- [led-task-pool-2] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:54:01
2021-05-28 18:54:02.387  INFO 12772 --- [led-task-pool-1] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:54:02
2021-05-28 18:54:02.409  INFO 12772 --- [led-task-pool-7] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:54:02
2021-05-28 18:54:03.391  INFO 12772 --- [led-task-pool-4] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:54:03
2021-05-28 18:54:04.386  INFO 12772 --- [led-task-pool-8] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:54:04
2021-05-28 18:54:05.387  INFO 12772 --- [led-task-pool-5] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:54:05
2021-05-28 18:54:05.423  INFO 12772 --- [led-task-pool-9] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Delay Task :: Execution Time - 18:54:05
2021-05-28 18:54:06.387  INFO 12772 --- [ed-task-pool-10] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task :: Execution Time - 18:54:06
2021-05-28 18:54:07.387  INFO 12772 --- [led-task-pool-6] c.t.basic.tech.info.app.ScheduledTasks   : Fixed Rate Task with Initial Delay :: Execution Time - 18:54:07

Download Source Code (Attached)

Kindly, let me know in case I missed anything. Enjoy :).