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個調度線程進行執行

 


歡迎點贊關注和收藏

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM