SpringBoot 2.2.5 配置自定義線程池,并使用@Async執(zhí)行異步方法,@Scheduled實現定時任務,及獲取線程池中線程的返回結果

說明

  1. 線程池是多線程的處理機制,線程池一般用于需要大量線程完成任務,并且完成時間較短時使用,大量用于并發(fā)框架和異步執(zhí)行任務。

優(yōu)點

  1. 降低資源消耗,通過利用已創(chuàng)建的線程降低線程創(chuàng)建和銷毀造成的消耗
  2. 有利于線程的可控性,如果線程無休止創(chuàng)建,會導致內存耗盡。
  3. 提高系統(tǒng)響應速度,通過使用已存在的線程,不需要等待新線程的創(chuàng)建就可以立即執(zhí)行當前任務。

主要參數簡單解釋

  1. corePoolSize:核心線程數,默認的核心線程的1,向線程池提交一個任務時,如果線程池已經創(chuàng)建的線程數小于核心線程數,即使此時存在空閑線程,也會通過創(chuàng)建一個新線程來執(zhí)行新任務,知道創(chuàng)建的線程等于核心線程數時,如果有空閑線程,則使用空閑線程。
  2. maxPoolSize:最大線程數,默認的最大線程數為Integer.MAX_VALUE 即231-1。當隊列滿了之后
  3. keepAliveSeconds:允許線程空閑時間,默認的線程空閑時間為60秒,當線程中的線程數大于核心線程數時,線程的空閑時間如果超過線程的存活時間,則此線程會被銷毀,直到線程池中的線程數小于等于核心線程數時。
  4. queueCapacity:緩沖隊列數,默認的緩沖隊列數是Integer.MAX_VALUE 即231-1,用于保存執(zhí)行任務的阻塞隊列
  5. allowCoreThreadTimeOut:銷毀機制,allowCoreThreadTimeOut為true則線程池數量最后銷毀到0個。allowCoreThreadTimeOut為false銷毀機制:超過核心線程數時,而且(超過最大值或者timeout過),就會銷毀。默認是false

完整代碼地址在結尾??!

第一步,配置application.yml,避免端口沖突

# 配置線程池
threadPoolTaskExecutor:
  corePoolSize: 10 # 核心線程數(默認線程數)
  maxPoolSize: 100 # 最大線程數
  keepAliveTime: 10 # 允許線程空閑時間(單位:默認為秒)
  queueCapacity: 200 # 緩沖隊列數
  threadNamePrefix: custom-executor- # 線程名統(tǒng)一前綴

server:
  port: 8099

spring:
  application:
    name: threadpool-demo-server

第二步,創(chuàng)建ThreadPoolTaskExecutorConfig配置類,如下

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @version 1.0
 * @author luoyu
 * @date 2019-08-09
 * @description 線程池配置
 */
@Configuration
@EnableAsync
@EnableScheduling
public class ThreadPoolTaskExecutorConfig {

    /**
     * 核心線程數(默認線程數)
     */
    @Value("${threadPoolTaskExecutor.corePoolSize}")
    private int corePoolSize;

    /**
     * 最大線程數
     */
    @Value("${threadPoolTaskExecutor.maxPoolSize}")
    private int maxPoolSize;

    /**
     * 允許線程空閑時間(單位:默認為秒)
     */
    @Value("${threadPoolTaskExecutor.keepAliveTime}")
    private int keepAliveTime;

    /**
     * 緩沖隊列數
     */
    @Value("${threadPoolTaskExecutor.queueCapacity}")
    private int queueCapacity;

    /**
     * 線程池名前綴
     */
    @Value("${threadPoolTaskExecutor.threadNamePrefix}")
    private String threadNamePrefix;

    /**
     * @return ThreadPoolTaskExecutor
     * @author jinhaoxun
     * @description 線程池配置,bean的名稱,默認為首字母小寫的方法名taskExecutor
     */
    @Bean("testTaskExecutor")
    public ThreadPoolTaskExecutor taskExecutor1() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //設置核心線程數
        executor.setCorePoolSize(corePoolSize);
        //設置最大線程數
        executor.setMaxPoolSize(maxPoolSize);
        //線程池所使用的緩沖隊列
        executor.setQueueCapacity(queueCapacity);
        //等待任務在關機時完成--表明等待所有線程執(zhí)行完
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 等待時間 (默認為0,此時立即停止),并沒等待xx秒后強制停止
        executor.setKeepAliveSeconds(keepAliveTime);
        // 線程名稱前綴
        executor.setThreadNamePrefix(threadNamePrefix);
        // 線程池對拒絕任務的處理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

}

說明

  1. @EnableAsync開啟@Async注解支持,也可以添加在啟動類上
  2. @EnableScheduling開啟@Scheduled注解支持,可以使用線程池配置定時任務,也可以添加在啟動類上

第三步,創(chuàng)建類服務類,TestService,TestServiceImpl,如下

TestService

public interface TestService {

    void test1();

    void test2();

    void test3();

    void test4();

}

TestServiceImpl

import com.luoyu.threadpool.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class TestServiceImpl implements TestService {

    @Resource(name = "testTaskExecutor")
    private ThreadPoolTaskExecutor testTaskExecutor;

    // 定時任務,一秒執(zhí)行一次
    @Scheduled(fixedRate  = 1000)
    @Override
    public void test1() {
        log.info("定時任務,一秒執(zhí)行一次");
    }

    @Override
    public void test2() {
        log.info("看看是哪個線程執(zhí)行了我!");
    }

    @Override
    public void test3() {
        testTaskExecutor.execute(() -> {
            log.info("看看是哪個線程執(zhí)行了我!");
        });
    }

    @Async("testTaskExecutor")
    @Override
    public void test4() {
        log.info("看看是哪個線程執(zhí)行了我!");
    }

}

第四步,創(chuàng)建類單元測試類,ThreadpoolApplicationTests,并進行測試,如下

import com.luoyu.threadpool.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@Slf4j
// 獲取啟動類,加載配置,確定裝載 Spring 程序的裝載方法,它回去尋找 主配置啟動類(被 @SpringBootApplication 注解的)
@SpringBootTest
class ThreadpoolApplicationTests {

    @Autowired
    private TestService testService;

    @Test
    void test2(){
        testService.test2();
    }

    @Test
    void test3(){
        testService.test3();
    }

    @Test
    void test4(){
        testService.test4();

    }

    @BeforeEach
    void testBefore(){
        log.info("測試開始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    }

    @AfterEach
    void testAfter(){
        log.info("測試結束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    }

}

第五步,測試定時任務的話,只需要啟動項目,查看控制臺日志即可

注意,@Async注解失效可能原因

  1. 沒有在@SpringBootApplication啟動類當中添加注解@EnableAsync注解
  2. 異步方法使用注解@Async的返回值只能為void或者Future
  3. 沒有走Spring的代理類。因為@Transactional和@Async注解的實現都是基于Spring的AOP,而AOP的實現是基于動態(tài)代理模式實現的。那么注解失效的原因就很明顯了,有可能因為調用方法的是對象本身而不是代理對象,因為沒有經過Spring容器

第六步,獲取線程池中線程的返回結果,修改TestService,TestServiceImpl新增方法,如下

TestService

public interface TestService {

    void test1();

    void test2();

    void test3();

    void test4();

    void test5() throws Exception;

}

TestServiceImpl

import com.luoyu.threadpool.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.Future;

@Slf4j
@Service
public class TestServiceImpl implements TestService {

    @Resource(name = "testTaskExecutor")
    private ThreadPoolTaskExecutor testTaskExecutor;

    // 定時任務,一秒執(zhí)行一次
    @Scheduled(fixedRate  = 1000)
    @Override
    public void test1() {
        log.info("定時任務,一秒執(zhí)行一次,看看是哪個線程執(zhí)行了我!{}", Thread.currentThread().getName());
    }

    @Override
    public void test2() {
        log.info("看看是哪個線程執(zhí)行了我!{}", Thread.currentThread().getName());
    }

    @Override
    public void test3() {
        for (int i = 0; i < 10; i++) {
            testTaskExecutor.execute(() -> {
                log.info("看看是哪個線程執(zhí)行了我!{}", Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    @Async("testTaskExecutor")
    @Override
    public void test4() {
        log.info("看看是哪個線程執(zhí)行了我!{}", Thread.currentThread().getName());
    }

    @Override
    public void test5() throws Exception {
        // 啟動兩個線程執(zhí)行子任務
        Future<Integer> count1 = testTaskExecutor.submit(() -> this.getCount1());
        Future<Integer> count2 = testTaskExecutor.submit(() -> this.getCount2());

        // 此處主線程進行阻塞
        Integer integer1 = count1.get();
        Integer integer2 = count2.get();

        // 拿到子線程返回結果
        log.info("1:" + integer1 + ",2:" + integer2);
    }
    
    private Integer getCount1() throws InterruptedException {
        Thread.sleep(5000);
        return 50;
    }

    private Integer getCount2() throws InterruptedException {
        Thread.sleep(3000);
        return 30;
    }

}

第七步,修改單元測試類,ThreadpoolApplicationTests,新增測試方法并進行測試,如下

import com.luoyu.threadpool.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@Slf4j
// 獲取啟動類,加載配置,確定裝載 Spring 程序的裝載方法,它回去尋找 主配置啟動類(被 @SpringBootApplication 注解的)
@SpringBootTest
class ThreadpoolApplicationTests {

    @Autowired
    private TestService testService;

    @Test
    void test2(){
        testService.test2();
    }

    @Test
    void test3(){
        testService.test3();
    }

    @Test
    void test4(){
        testService.test4();
    }

    @Test
    void test5() throws Exception {
        testService.test5();
    }

    @BeforeEach
    void testBefore(){
        log.info("測試開始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    }

    @AfterEach
    void testAfter(){
        log.info("測試結束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    }

}

完整代碼地址:https://github.com/Jinhx128/springboot-demo

注:此工程包含多個module,本文所用代碼均在threadpool-demo模塊下

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容