SpringBoot定時任務多線程實現示例

測試Spring Boot定時任務沖突時,使用的線程數量

引入依賴:

Spring Boot 2.6.1

 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
 </dependency>

簡單的測試類

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author Song Jiangtao
 * @date 2021/12/22
 * @description:
 */
@Component
@Slf4j
public class Task {

    @Scheduled(cron="*/2 * * * * ?")
    public void process(){
        log.info("do something");
    }

    @Scheduled(fixedRate = 2000)
    public void currentTime(){
        log.info("做點什麼");
    }
}

啟動類開啟定時任務:@EnableScheduling

測試結果如下:

在這裡插入圖片描述

由結果可見,main線程啟動以後和scheduling-1線程跑瞭兩個任務

由此可見,Spring Boot 定時器 默認是 單線程

我們新增兩個定時任務來看看這樣會有什麼後果

@Scheduled(cron="*/2 * * * * ?")
public void process2() throws InterruptedException {
     Thread.sleep(5000L);
     log.info("do something use long time");
 }
 @Scheduled(cron="*/2 * * * * ?")
 public void process3() throws InterruptedException {
     Thread.sleep(10000L);
     log.info("do something use long long time");
 }
2021-12-22 16:30:28.735  INFO 14520 --- [           main] c.e.s.SpringBootScheduledApplication     : Starting SpringBootScheduledApplication using Java 1.8.0_202 on TCZ-D with PID 14520 (D:\code\SpringBootScheduled\target\classes started by Administrator in D:\code\SpringBootScheduled)
2021-12-22 16:30:28.738  INFO 14520 --- [           main] c.e.s.SpringBootScheduledApplication     : No active profile set, falling back to default profiles: default
2021-12-22 16:30:29.315  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:29.318  INFO 14520 --- [           main] c.e.s.SpringBootScheduledApplication     : Started SpringBootScheduledApplication in 0.937 seconds (JVM running for 3.068)
2021-12-22 16:30:30.002  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:30:40.003  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something use long long time
2021-12-22 16:30:45.005  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something use long time
2021-12-22 16:30:45.005  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:45.005  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:55.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something use long long time
2021-12-22 16:30:55.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:55.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:30:55.007  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something

很明顯任務會發生錯亂,嚴重時會導致線程阻塞,最後崩潰

解決方法:引入線程池

新增配置類,配置線程池

package com.example.springbootscheduled.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Song Jiangtao
 * @date 2021/12/22
 * @description:
 */
@Configuration
@EnableAsync
public class TaskConfig {

    /**
     * 默認線程數
     */
    private static final int corePoolSize = 10;
    /**
     * 最大線程數
     */
    private static final int maxPoolSize = 100;
    /**
     * 允許線程空閑時間(單位:默認為秒),十秒後就把線程關閉
     */
    private static final int keepAliveTime = 10;
    /**
     * 緩沖隊列數
     */
    private static final int queueCapacity = 200;
    /**
     * 線程池名前綴
     */
    private static final String threadNamePrefix = "task-thread-";

    /**
     * bean的名稱,默認為 首字小寫 方法名
     */
    @Bean("taskExecutor")
    public ThreadPoolTaskExecutor getThread() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(keepAliveTime);
        executor.setKeepAliveSeconds(queueCapacity);
        executor.setThreadNamePrefix(threadNamePrefix);
        //線程池拒絕策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}

每個定時任務添加註解 @Async("taskExecutor")

@Async("taskExecutor")
@Scheduled(fixedRate = 2000)
public void currentTime(){
    log.info("做點什麼");
}

啟動測試

2021-12-22 16:41:36.141  INFO 19424 --- [           main] c.e.s.SpringBootScheduledApplication     : Starting SpringBootScheduledApplication using Java 1.8.0_202 on TCZ-D with PID 19424 (D:\code\SpringBootScheduled\target\classes started by Administrator in D:\code\SpringBootScheduled)
2021-12-22 16:41:36.143  INFO 19424 --- [           main] c.e.s.SpringBootScheduledApplication     : No active profile set, falling back to default profiles: default
2021-12-22 16:41:36.991  INFO 19424 --- [           main] c.e.s.SpringBootScheduledApplication     : Started SpringBootScheduledApplication in 1.325 seconds (JVM running for 3.392)
2021-12-22 16:41:37.008  INFO 19424 --- [  task-thread-1] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:41:38.001  INFO 19424 --- [  task-thread-2] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:38.991  INFO 19424 --- [  task-thread-5] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:41:40.003  INFO 19424 --- [  task-thread-8] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:40.991  INFO 19424 --- [  task-thread-9] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:41:42.001  INFO 19424 --- [ task-thread-10] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:42.989  INFO 19424 --- [  task-thread-5] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:41:43.002  INFO 19424 --- [  task-thread-3] c.e.springbootscheduled.scheduled.Task   : do something use long time
2021-12-22 16:41:44.002  INFO 19424 --- [  task-thread-9] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:44.990  INFO 19424 --- [  task-thread-5] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:41:45.002  INFO 19424 --- [  task-thread-6] c.e.springbootscheduled.scheduled.Task   : do something use long time
2021-12-22 16:41:46.001  INFO 19424 --- [  task-thread-9] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:46.990  INFO 19424 --- [  task-thread-6] c.e.springbootscheduled.scheduled.Task   : 做點什麼
2021-12-22 16:41:47.002  INFO 19424 --- [  task-thread-1] c.e.springbootscheduled.scheduled.Task   : do something use long time
2021-12-22 16:41:48.002  INFO 19424 --- [  task-thread-4] c.e.springbootscheduled.scheduled.Task   : do something use long long time

到此這篇關於SpringBoot定時任務多線程實現示例的文章就介紹到這瞭,更多相關SpringBoot定時任務多線程內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: