bug
zhangzeli
2022-01-17 e151c7c3e105358abe1f84f10f09b12e3430915b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cn.exrick.xboot.core.common.utils;
 
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
/**
 * @author Exrickx
 */
public class ThreadPoolUtil {
 
    /**
     * 线程缓冲队列
     */
    private static BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<>(100);
 
    /**
     * 核心线程数,会一直存活,即使没有任务,线程池也会维护线程的最少数量
     */
    private static final int SIZE_CORE_POOL = 5;
 
    /**
     * 线程池维护线程的最大数量
     */
    private static final int SIZE_MAX_POOL = 10;
 
    /**
     * 当前线程数超过核心线程数时,空闲线程存活时间
     */
    private static final long ALIVE_TIME = 2000;
 
    private static ThreadPoolExecutor pool = new ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL, ALIVE_TIME, TimeUnit.MILLISECONDS,
            bqueue, new ThreadPoolExecutor.CallerRunsPolicy());
 
    static {
 
        pool.prestartAllCoreThreads();
    }
 
    public static ThreadPoolExecutor getPool() {
        return pool;
    }
 
    private ThreadPoolUtil() {
        // 工具类建议不提供public构造入口
        throw new IllegalStateException("Utility class");
    }
}