ThreadPoolExecutor學習筆記

背景

在開發(fā)使用spring boot 時,直接注入TaskExecutor就可以使用了,然后想知道spring boot是哪里自動注入了。

  1. 首先在網(wǎng)上的資料看資料,知道spring默認使用的是ThreadPoolTaskExecutor,于是去看源碼,如下:
public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
        implements AsyncListenableTaskExecutor, SchedulingTaskExecutor 
  1. 因為我不知道是在哪里初始化的,所以在ThreadPoolTaskExecutor和ExecutorConfigurationSupport各處set的方法中打了斷點。


    image.png

    可以看到是在TaskExecutionAutoConfiguration初始化的。

    @Bean
    @ConditionalOnMissingBean
    public TaskExecutorBuilder taskExecutorBuilder() {
        TaskExecutionProperties.Pool pool = this.properties.getPool();
        TaskExecutorBuilder builder = new TaskExecutorBuilder();
        builder = builder.queueCapacity(pool.getQueueCapacity());
        builder = builder.corePoolSize(pool.getCoreSize());
        builder = builder.maxPoolSize(pool.getMaxSize());
        builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout());
        builder = builder.keepAlive(pool.getKeepAlive());
        builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix());
        builder = builder.customizers(this.taskExecutorCustomizers);
        builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
        return builder;
    }

    @Lazy
    @Bean(name = { APPLICATION_TASK_EXECUTOR_BEAN_NAME,
            AsyncAnnotationBeanPostProcessor.DEFAULT_TASK_EXECUTOR_BEAN_NAME })
    @ConditionalOnMissingBean(Executor.class)
    public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
        return builder.build();
    }
  1. 看到這里有點好奇corePoolSize和maxPoolSize的區(qū)別,因為在ThreadPoolTaskExecutor中maxPoolSize是Integer最大值。以為我沒有設置,那就會使用Integer最大值,這樣的線程池還有什么意義呢?

  2. 于是找到了官方說法

Core and maximum pool sizes
A ThreadPoolExecutor will automatically adjust the pool size (see getPoolSize()) according to the bounds set by corePoolSize (see getCorePoolSize()) and maximumPoolSize (see getMaximumPoolSize()). When a new task is submitted in method execute(java.lang.Runnable), and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool. By setting maximumPoolSize to an essentially unbounded value such as Integer.MAX_VALUE, you allow the pool to accommodate an arbitrary number of concurrent tasks. Most typically, core and maximum pool sizes are set only upon construction, but they may also be changed dynamically using setCorePoolSize(int) and setMaximumPoolSize(int).

大概意思是說當一個新的任務提交時,如果當前線程小于corePoolSize那么就會新創(chuàng)建一個線程來處理請求,不管工作線程是否是空閑。當線程數(shù)量大于corePoolSize并且小于maxPoolSize時,則只有隊列滿的時候才會創(chuàng)建新線程。如果把corePoolSize和maxPoolSize設置為同樣大小,那么就是一個固定大小的線程池。通過把maxPoolSize設置為Integer最大值,基本就允許任意數(shù)量任務。

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

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容