原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/14933829.html
Project Directory
Maven Dependency
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <relativePath/> </parent> <groupId>org.fool</groupId> <artifactId>hellospring</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
server.port=8080
SRC
ScheduleConfiguration.java
package org.fool.core.config; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @EnableScheduling public class ScheduleConfiguration { }
Scheduler.java
package org.fool.core.schedule; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @Slf4j public class Scheduler { @Scheduled(cron = "0/5 * * * * ?") public void mockMethod1() { long threadId = Thread.currentThread().getId(); String threadName = Thread.currentThread().getName(); log.info("mockMethod1 start with current thread id: {}, name: {}", threadId, threadName); try { log.info("mockMethod1 sleep with current thread id: {}, name: {}", threadId, threadName); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } log.info("mockMethod1 end with current thread id: {}, name: {}", threadId, threadName); } @Scheduled(cron = "0/5 * * * * ?") public void mockMethod2() { long threadId = Thread.currentThread().getId(); String threadName = Thread.currentThread().getName(); log.info("mockMethod2 start with current thread id: {}, name: {}", threadId, threadName); try { log.info("mockMethod2 sleep with current thread id: {}, name: {}", threadId, threadName); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } log.info("mockMethod2 end with current thread id: {}, name: {}", threadId, threadName); } }
Note: 模拟2个方法,每5秒执行一次,方法执行过程中sleep 10s后,结束执行。
Application.java
package org.fool.core; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Run
Note:从运行结果可以看出只有1个调度线程分别处理两个方法的执行,这是因为SpringBoot的调度默认配置了1个线程
所以这边只要在application.properties 中添加如下几个参数
server.port=8080 spring.task.scheduling.thread-name-prefix=mock-task-schedule- spring.task.scheduling.pool.size=8 spring.task.scheduling.shutdown.await-termination=true
重启后再次运行,可以看到两个方法分别被安排了2个调度线程进行执行
欢迎点赞关注和收藏