springboot自定义线程池

在一个应用程序中,我们需要多次使用线程,也就意味着,我们需要多次创建并销毁线程。而创建并销毁线程的过程势必会消耗内存。而在Java中,内存资源是及其宝贵的,所以,我们就提出了线程池的概念。线程池的好处,就是可以方便的管理线程,也可以减少内存的消耗。

四种常见的线程池:

CachedThreadPool:可缓存的线程池,该线程池中没有核心线程,非核心线程的数量为Integer.max_value,就是无限大,当有需要时创建线程来执行任务,没有需要时回收线程,适用于耗时少,任务量大的情况。

SecudleThreadPool:周期性执行任务的线程池,按照某种特定的计划执行线程中的任务,有核心线程,但也有非核心线程,非核心线程的大小也为无限大。适用于执行周期性的任务。

SingleThreadPool:只有一条线程来执行任务,适用于有顺序的任务的应用场景。

FixedThreadPool:定长的线程池,有核心线程,核心线程的即为最大的线程数量,没有非核心线程。

/**
 * @auther fanxuebo
 * @desc    线程池配置和初始化
 * @Company 
 * @create 2018/12/29 8:23
 */
@Configuration
public class MyExecutorPool {

    @Autowired
    private ThreadPoolProperties threadPoolProperties;

    @Bean(name = "myAsyncThread")
    public ThreadPoolTaskExecutor myTaskAsyncPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(threadPoolProperties.getCorePoolSize());//表示线程池核心线程,正常情况下开启的线程数量。
        executor.setMaxPoolSize(threadPoolProperties.getMaxPoolSize());//当核心线程都在跑任务,还有多余的任务会存到此处。
        executor.setQueueCapacity(threadPoolProperties.getQueueCapacity());//如果queueCapacity存满了,还有任务就会启动更多的线程,直到线程数达到maxPoolSize。如果还有任务,则根据拒绝策略进行处理。
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//该策略是又调用任务的线程执行。
        executor.setKeepAliveSeconds(threadPoolProperties.getKeepAliveSeconds());//非核心线程的超时时长,超长后会被回收。
        executor.setThreadNamePrefix(threadPoolProperties.getThreadNamePrefix());
        executor.initialize();//初始化线程池。
        return executor;
    }
}


 

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @auther fanxuebo
 * @desc    配置线程池参数读取配置文件
 * @Company 
 * @create 2018/12/29 8:27
 */
@ConfigurationProperties("executor")
@Component
public class ThreadPoolProperties {

    private Integer corePoolSize;
    private Integer maxPoolSize;
    private Integer queueCapacity;
    private Integer keepAliveSeconds;
    private String threadNamePrefix;

    public Integer getCorePoolSize() {
        return corePoolSize;
    }

    public void setCorePoolSize(Integer corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public Integer getMaxPoolSize() {
        return maxPoolSize;
    }

    public void setMaxPoolSize(Integer maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    public Integer getQueueCapacity() {
        return queueCapacity;
    }

    public void setQueueCapacity(Integer queueCapacity) {
        this.queueCapacity = queueCapacity;
    }

    public Integer getKeepAliveSeconds() {
        return keepAliveSeconds;
    }

    public void setKeepAliveSeconds(Integer keepAliveSeconds) {
        this.keepAliveSeconds = keepAliveSeconds;
    }

    public String getThreadNamePrefix() {
        return threadNamePrefix;
    }

    public void setThreadNamePrefix(String threadNamePrefix) {
        this.threadNamePrefix = threadNamePrefix;
    }
}

配置文件:

executor:
    corePoolSize: 5
    maxPoolSize: 10
    queueCapacity: 20
    keepAliveSeconds: 60
    threadNamePrefix: XCExecutor-

1、在springboot启动类上加@EnableAsync注解

2、然后在需要多线程执行的方法上加注解@Async(value = "myAsyncThread")即可。

猜你喜欢

转载自blog.csdn.net/fanxb92/article/details/87010804