chenjiahe
2023-01-10 bcc2fa8323a61920c7d2a6139c38fc25b17baa7f
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import org.springframework.stereotype.Component;
4
5 import java.util.concurrent.*;
6
7 /**
8  * @PackageName com.hx.util
9  * @ProjectName hx-parent
10  * @Author: ChenJiaHe
11  * @Date: Create in 17:12 2019/8/6
12  * @Description:
13  * @Copyright Copyright (c) 2019, hx01@163.com All Rights Reserved.
14  */
15 @Component
16 public class ThreadPoolUtils {
17
18     public static BlockingQueue<Runnable> queueToUse = new LinkedBlockingQueue<>(120);
19
20     /**
21      * @param poolSize
22      * @param method
23      * @MethodName creatExeutorService
24      * @Description 创建线程池方法
25      * @Auther ChenJiaHe
26      * @Date 2019/8/6 17:19
27      * @Since JDK 1.8
28      */
29     public ExecutorService creatExeutorService(int poolSize, String method, BlockingQueue<Runnable> queueToUse) {
30         int coreSize = Runtime.getRuntime().availableProcessors();
31         if (poolSize < coreSize) {
32             coreSize = poolSize;
33         }
34         ThreadFactory threadFactory = r -> {
35             Thread t = new Thread(r, "thread created mewthod{" + method + "}");
36             t.setDaemon(true);
37             return t;
38         };
39         final ThreadPoolExecutor executor = new ThreadPoolExecutor(coreSize, poolSize, 60, TimeUnit.SECONDS, queueToUse, threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
40         return executor;
41     }
42 }