SpringBoot Schedule 調整默認調度線程數

Project Directory

 

Maven Dependency

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk= watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=
<?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.javajava

package org.fool.core.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class ScheduleConfiguration {

}

 

Scheduler.javaweb

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後,結束執行。spring

 

Application.javaapache

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個線程app

 

因此這邊只要在application.properties 中添加以下幾個參數maven

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

 


 

相關文章
相關標籤/搜索