簡體   English   中英

Spring 引導異步執行程序創建的線程數超過核心池大小和最大池大小?

[英]Spring boot async executor is creating more threads than core pool size and max pool size?

我正在使用 spring 啟動異步配置來異步運行某些功能。

Spring 引導創建的線程多於 corePoolSize 和 maxPoolSize 我已經給了兩個相同的值來定義固定線程池。

但是 spring 創建了更多線程。

見下圖取自 Jconsole 顯示當前正在運行的線程數的 Jconsole 屏幕截圖

它正在創建 Async-57、Async-58、Async-59……

它只是給出隨機數還是創建第 57 個線程或第 58 個線程(即使有 30 個固定線程)?

請幫助我,因為這將投入生產,POD 資源有限。 這是我的配置。 我沒有使用隊列大小,因為在生產中請求是不明確的。 我正在使用隊列的默認大小 INTEGER.MAX


    @Value("${async.config.corepoolsize}")
    private String corePoolSize;
    
    @Value("${async.config.maxpoolsize}")
    private String maxPoolSize;
        
    @Bean("asyncExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Integer.parseInt(corePoolSize));
        executor.setMaxPoolSize(Integer.parseInt(maxPoolSize));
        executor.setKeepAliveSeconds(30);
        executor.setAllowCoreThreadTimeOut(true);
        executor.setThreadNamePrefix("Async-");
        executor.initialize();
        return executor;
    }


使用更多線程,因為它們僅用於 IO 次操作(我將此配置值保存在 app.properties 文件中)


#Async configuration for IO operations
async.config.corepoolsize=30
async.config.maxpoolsize=30

您可以通過將核心和最大池大小設置為 2 並發送 20 個事件進行處理來測試此配置。 在這種情況下,理想情況下線程名稱應限制為 2。 也嘗試在日志中打印 Thread.currentThread() 值。

盡管我沒有在我的線程池中使用 executor.setKeepAliveSeconds(30) 和 executor.setAllowCoreThreadTimeOut(true),但配置看起來是正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM