SpringBoot Schedule 调整默认调度线程数
2021/6/26 23:59:32
本文主要是介绍SpringBoot Schedule 调整默认调度线程数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
原创转载请注明出处: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>View Code
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个调度线程进行执行
欢迎点赞关注和收藏
这篇关于SpringBoot Schedule 调整默认调度线程数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-10百万架构师第十三课:源码分析:Spring 源码分析:Spring核心IOC容器及依赖注入原理|JavaGuide
- 2025-01-10便捷好用的电商API工具合集
- 2025-01-09必试!帮 J 人团队解决物流错发漏发的软件神器!
- 2025-01-09不容小觑!助力 J 人物流客服安抚情绪的软件!
- 2025-01-09为什么医疗团队协作离不开智能文档工具?
- 2025-01-09惊叹:J 人团队用啥软件让物流服务快又准?
- 2025-01-09如何利用数据分析工具优化项目资源分配?4种工具推荐
- 2025-01-09多学科协作难?这款文档工具可以帮你省心省力
- 2025-01-09团队中的技术项目经理TPM:工作内容与资源优化策略
- 2025-01-09JIT生产管理法:优化流程,提升竞争力的秘诀