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 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 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; } }