chenjiahe
2022-03-31 b5bfe58beeeeb8343bdea6a9412f8f6704b67633
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
package com.hx.util;
 
import org.springframework.stereotype.Component;
 
import java.util.concurrent.*;
 
/**
 * @PackageName com.hx.util
 * @ProjectName hx-parent
 * @Author: ChenJiaHe
 * @Date: Create in 17:12 2019/8/6
 * @Description:
 * @Copyright Copyright (c) 2019, hx01@163.com All Rights Reserved.
 */
@Component
public class ThreadPoolUtils {
 
    public static BlockingQueue<Runnable> queueToUse = new LinkedBlockingQueue<>(120);
 
    /**
     * @param poolSize
     * @param method
     * @MethodName creatExeutorService
     * @Description 创建线程池方法
     * @Auther ChenJiaHe
     * @Date 2019/8/6 17:19
     * @Since JDK 1.8
     */
    public ExecutorService creatExeutorService(int poolSize, String method, BlockingQueue<Runnable> queueToUse) {
        int coreSize = Runtime.getRuntime().availableProcessors();
        if (poolSize < coreSize) {
            coreSize = poolSize;
        }
        ThreadFactory threadFactory = r -> {
            Thread t = new Thread(r, "thread created mewthod{" + method + "}");
            t.setDaemon(true);
            return t;
        };
        final ThreadPoolExecutor executor = new ThreadPoolExecutor(coreSize, poolSize, 60, TimeUnit.SECONDS, queueToUse, threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}