SpringBoot 2.2.5 配置自定义线程池,并使用@Async执行异步方法,@Scheduled实现定时任务,及获取线程池中线程的返回结果

说明

  1. 线程池是多线程的处理机制,线程池一般用于需要大量线程完成任务,并且完成时间较短时使用,大量用于并发框架和异步执行任务。

优点

  1. 降低资源消耗,通过利用已创建的线程降低线程创建和销毁造成的消耗
  2. 有利于线程的可控性,如果线程无休止创建,会导致内存耗尽。
  3. 提高系统响应速度,通过使用已存在的线程,不需要等待新线程的创建就可以立即执行当前任务。

主要参数简单解释

  1. corePoolSize:核心线程数,默认的核心线程的1,向线程池提交一个任务时,如果线程池已经创建的线程数小于核心线程数,即使此时存在空闲线程,也会通过创建一个新线程来执行新任务,知道创建的线程等于核心线程数时,如果有空闲线程,则使用空闲线程。
  2. maxPoolSize:最大线程数,默认的最大线程数为Integer.MAX_VALUE 即231-1。当队列满了之后
  3. keepAliveSeconds:允许线程空闲时间,默认的线程空闲时间为60秒,当线程中的线程数大于核心线程数时,线程的空闲时间如果超过线程的存活时间,则此线程会被销毁,直到线程池中的线程数小于等于核心线程数时。
  4. queueCapacity:缓冲队列数,默认的缓冲队列数是Integer.MAX_VALUE 即231-1,用于保存执行任务的阻塞队列
  5. allowCoreThreadTimeOut:销毁机制,allowCoreThreadTimeOut为true则线程池数量最后销毁到0个。allowCoreThreadTimeOut为false销毁机制:超过核心线程数时,而且(超过最大值或者timeout过),就会销毁。默认是false

完整代码地址在结尾!!

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

# 配置线程池
threadPoolTaskExecutor:
  corePoolSize: 10 # 核心线程数(默认线程数)
  maxPoolSize: 100 # 最大线程数
  keepAliveTime: 10 # 允许线程空闲时间(单位:默认为秒)
  queueCapacity: 200 # 缓冲队列数
  threadNamePrefix: custom-executor- # 线程名统一前缀

server:
  port: 8099

spring:
  application:
    name: threadpool-demo-server

第二步,创建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);
        //等待任务在关机时完成--表明等待所有线程执行完
        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注解支持,可以使用线程池配置定时任务,也可以添加在启动类上

第三步,创建类服务类,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;

    // 定时任务,一秒执行一次
    @Scheduled(fixedRate  = 1000)
    @Override
    public void test1() {
        log.info("定时任务,一秒执行一次");
    }

    @Override
    public void test2() {
        log.info("看看是哪个线程执行了我!");
    }

    @Override
    public void test3() {
        testTaskExecutor.execute(() -> {
            log.info("看看是哪个线程执行了我!");
        });
    }

    @Async("testTaskExecutor")
    @Override
    public void test4() {
        log.info("看看是哪个线程执行了我!");
    }

}

第四步,创建类单元测试类,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的实现是基于动态代理模式实现的。那么注解失效的原因就很明显了,有可能因为调用方法的是对象本身而不是代理对象,因为没有经过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;

    // 定时任务,一秒执行一次
    @Scheduled(fixedRate  = 1000)
    @Override
    public void test1() {
        log.info("定时任务,一秒执行一次,看看是哪个线程执行了我!{}", Thread.currentThread().getName());
    }

    @Override
    public void test2() {
        log.info("看看是哪个线程执行了我!{}", Thread.currentThread().getName());
    }

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

    @Async("testTaskExecutor")
    @Override
    public void test4() {
        log.info("看看是哪个线程执行了我!{}", Thread.currentThread().getName());
    }

    @Override
    public void test5() throws Exception {
        // 启动两个线程执行子任务
        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模块下

  • 14
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值