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 }