SpringBoot普通消息隊列線程池配置


 1 package com.liuhuan.study.config;
 2 
 3 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 
 7 import java.util.concurrent.*;
 8 
 9 /**
10  * @author LiuHuan
11  * @date 2019-10-08 15:42
12  * @desc 線程池
13  */
14 @Configuration
15 public class ThreadPoolConfig {
16 
17     /**
18      * 線程池維護線程的最少數量
19      */
20     private static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();
21 
22     /**
23      * 線程池維護線程的最大數量
24      */
25     private static final int MAX_POOL_SIZE = 20;
26 
27     /**
28      * 空閑線程等待工作的超時時間
29      */
30     private static final long KEEP_ALIVE_TIME = 1000L;
31 
32     /**
33      * 線程池所使用的緩沖隊列大小
34      */
35     private final static int WORK_QUEUE_SIZE = 10;
36 
37 
38 
39     /**
40      * 消息隊列線程池
41      */
42     @Bean(value = "processThreadPool")
43     public ExecutorService processThreadPool(){
44         ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
45             .setNameFormat("process-thread-%d").build();
46 
47         ExecutorService pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
48             new ArrayBlockingQueue<Runnable>(WORK_QUEUE_SIZE), namedThreadFactory, new ThreadPoolExecutor.DiscardOldestPolicy());
49         return pool ;
50     }
51 
52 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM