广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cn.huge.base.common.utils;
 
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
 
 
/**
 * @title: 线程池工具类
 * @description: 线程池工具类
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
public class ThreadPoolUtils {
    /**
     * 获取处理器数量
     */
    private static final int CPUNUM = Runtime.getRuntime().availableProcessors();
 
    //private static final int MAXMUNPOOLSIZE = 10;
    /**
     * 线程池最大容纳线程数,通过处理器数量来确定最佳线程数量的大小
     */
    private static final int MAXMUNPOOLSIZE = 2*CPUNUM+1;
 
    //private static final int COREPOOLSIZE = 5;
    /**
     * 核心线程数量大小
     */
    private static final int COREPOOLSIZE = 2*CPUNUM;
 
    /**
     * 线程空闲后的存活时长
     */
    private static final int KEEPALIVETIME = 30;
 
    private static ThreadPoolExecutor executor = null;
 
    private static ThreadPoolExecutor getExecutor(){
        if(executor == null){
            synchronized (ThreadPoolExecutor.class){
                if(executor == null){
                    executor = new ThreadPoolExecutor(
                            COREPOOLSIZE,
                            MAXMUNPOOLSIZE,
                            KEEPALIVETIME,
                            //KeepAliveTime的时间单位(秒)
                            TimeUnit.SECONDS,
                            //任务过多时存储任务的一个阻塞队列
                            //new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE),
                            new LinkedBlockingDeque<Runnable>(500),
                            new ThreadFactory() {
                                private final AtomicInteger atomicInteger = new AtomicInteger(1); //初始计数为0
                                @Override
                                public Thread newThread(Runnable runnable) {
                                    // getAndIncrement以原子的操作递增当前值并返回旧值。相当于++i操作
                                    return new Thread(runnable,"ThreadPoolUtil-" + atomicInteger.getAndIncrement());
                                }
                            },   //线程的创建工厂
                            //线程池任务满载后采取的任务拒绝策略
                            //new ThreadPoolExecutor.DiscardOldestPolicy()
                            //线程池满载后采取由提交任务者执行这个任务的策略
                            new ThreadPoolExecutor.CallerRunsPolicy()
                    );
                    //设置核心线程空闲时间超过keepAliveTime值时释放线程
                    executor.allowCoreThreadTimeOut(true);
                }
            }
        }
        return executor;
    }
 
    /**
     * 无返回值直接执行
     * @param runnable
     */
    public static void executor(Runnable runnable){
        getExecutor().execute(runnable);
    }
 
 
    /**
     * 有返回值
     * @param callable
     * @param <T>
     * @return
     */
    public static <T> Future<T> submit(Callable<T> callable){
        return getExecutor().submit(callable);
    }
}