• Spring/SpringBoot自定义线程池


    Spring/SpringBoot自定义线程池

    在 Spring/SpringBoot 中,可以使用 @Configuration 和 @Bean 去设置线程池,用 @Value 去做线程池的参数配置。

    依赖包:

    引用 google 的 guava包。

    
        com.google.guava
        guava
        19.0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    线程池配置:

    import com.google.common.util.concurrent.ThreadFactoryBuilder;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.concurrent.*;
    
    
    
    @Configuration
    public class ThreadPoolExecutorConfig {
    
        /**
         * 核心线程数, :冒号后面是默认值
         */
        @Value("${executor.core.pool.size:20}")
        private int corePoolSize;
    
        /**
         * 最大线程数, :冒号后面是默认值
         */
        @Value("${executor.max.pool.size:100}")
        private int maxPoolSize;
    
        /**
         * 线程池中空闲线程等待工作的超时时间。也就是没有任务执行时,线程的存活时间。
         */
        @Value("${executor.keep.alive.time:2}")
        private int keepAliveTime;
    
        /**
         * 阻塞队列长度
         */
        @Value("${executor.blocking.queue.size:200}")
        private int blockingQueueSize;
    
    
        /**
         * 使用 @Bean 指定线程池的名称
         *
         */
        @Bean("myExecutor")
        public ThreadPoolExecutor myExecutor() {
    
            //阻塞队列长度
            BlockingQueue workQueue = new ArrayBlockingQueue<>(blockingQueueSize);
    
            //创建线程或线程池时指定有意义的线程名称,方便出错时回溯。名称如果过长,会被截断。
            ThreadFactory threadFactory = new ThreadFactoryBuilder()
                    .setNameFormat("my-executor-%d").build();
            //可以自定义拒绝策略,也可以不加这个参数,构造方法会直接用默认的拒绝策略。
            //核心业务,可以使用 CallerRunsPolicy 策略,非核心业务,使用 AbortPolicy 策略。
            ThreadPoolExecutor.CallerRunsPolicy abortPolicy = new ThreadPoolExecutor.CallerRunsPolicy();
    
            return new ThreadPoolExecutor(corePoolSize, maxPoolSize,
                    keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, abortPolicy);
    
    
        }
    
    
        /**
         * 不同的业务,有时可以使用不同的线程池、不同的配置。
         *
         */
        @Bean("myExecutor2")
        public ThreadPoolExecutor myExecutor2() {
    
            //阻塞队列长度
            BlockingQueue workQueue = new ArrayBlockingQueue<>(blockingQueueSize);
    
            //创建线程或线程池时指定有意义的线程名称,方便出错时回溯。
            ThreadFactory threadFactory = new ThreadFactoryBuilder()
                    .setNameFormat("my-executor2-%d").build();
            //可以自定义拒绝策略,也可以不加这个参数,构造方法会直接用默认的拒绝策略
            //核心业务,可以使用 CallerRunsPolicy 策略,非核心业务,使用 AbortPolicy 策略。
            ThreadPoolExecutor.CallerRunsPolicy abortPolicy = new ThreadPoolExecutor.CallerRunsPolicy();
    
            return new ThreadPoolExecutor(corePoolSize, maxPoolSize,
                    keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, abortPolicy);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    配置参数:

    可以写到配置中心里面,如果没有配置中心,放到 propeties 文件也行。

    executor.core.pool.size=20
    executor.max.pool.size=100
    executor.keep.alive.time=2
    executor.blocking.queue.size=200
    
    • 1
    • 2
    • 3
    • 4

    使用线程池:

    日志框架自行补充,可以在Service类上面用 lombok的 @Slf4j 注解,也可以用其他的日志。

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.concurrent.*;
    
    
    @Service
    @Slf4j
    public class MyService {
    
        @Resource(name = "myExecutor")
        private ThreadPoolExecutor executor;
    
        public void execute() {
            executor.execute(() -> log.info("线程池执行任务."));
    
        }
    
        public void submit() throws Exception {
            Future future = executor.submit(() -> {
                log.info("线程池执行异步任务.");
                return "okk";
            });
            System.out.println("异步任务返回:" + future.get(3, TimeUnit.SECONDS));
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    运行,查看日志:

    INFO 18876 --- [ my-executor-0] com.example.demo.threadpool.MyService    : 线程池执行任务.
    
    • 1

    可以看到 线程池已经执行任务。日志中的 线程名称 是 my-executor 开头的。

    把MyService 类中的 (name = “myExecutor”) 换成 (name = “myExecutor2”) ,再次运行,查看日志:

    INFO 18876 --- [ my-executor2-0] com.example.demo.threadpool.MyService    : 线程池执行任务.
    
    • 1

    可以看到日志,线程池已经执行任务。 线程名称 变成 了 my-executor2 开头的了。

    创建线程或线程池时,指定有意义的线程名称,方便出错时回溯。

    Spring / 普通Java项目的自定义线程池

    详情见:https://www.cnblogs.com/expiator/p/17140760.html

  • 相关阅读:
    射频问答精选 | 网络分析仪的问题汇总和解答
    ESP32控制器使用SX1278 LoRa模块的方法
    c++ grpc 第一个用例
    haproxy
    对象和数据结构
    算法-买卖股票的最佳时机II
    xCode14.3.1运行MonkeyDev出现“Executable Not Found“的解决办法
    Linux: vi 编辑器
    Debian 11 更新 Node.js 版本
    Python如何实现模板方法设计模式?什么是模板方法设计模式?Python 模板方法设计模式示例代码
  • 原文地址:https://blog.csdn.net/sinat_32502451/article/details/133959765