Java并发编程实战~线程池创建

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

/**
 * TODO 在此写上类的相关说明.<br>
 * @author gqltt<br>
 * @version 1.0.0 2021年11月29日<br>
 * @see 
 * @since JDK 1.5.0
 */
public class ThreadPoolExecutorBuilder {
	
	/**
	 * 单例.
	 */
	private static final ThreadPoolExecutorBuilder instance = 
			new ThreadPoolExecutorBuilder();
	/**
	 * 线程池.
	 */
	private static Map<String, ThreadPoolExecutor> threadPoolMap = 
			new ConcurrentHashMap<>();
	
	/**
	 * 获取单例.
	 * @return
	 */
	public static ThreadPoolExecutorBuilder getInstance() {
		return instance;
	}
	
	/**
	 * 获取线程池对象.
	 * @param threadNamePrefix 线程名称前缀.
	 * @param corePoolSize 核心线程数量.
	 * @param maxPoolSize 最大线程数量.
	 * @param keepAliveSeconds 空闲秒数.
	 * @param queueSize 阻塞队列长度.
	 * @return 线程池对象.
	 */
	public ThreadPoolExecutor getThreadPoolExecutor(final String threadNamePrefix, 
			final int corePoolSize, final int maxPoolSize, 
			final long keepAliveSeconds, final int queueSize) {

		return getThreadPoolExecutor(threadNamePrefix, 
				corePoolSize, maxPoolSize, 
				keepAliveSeconds, queueSize, 
				new ThreadPoolExecutor.AbortPolicy());
	}
	
	/**
	 * 获取线程池对象.
	 * @param threadNamePrefix 线程名称前缀.
	 * @param corePoolSize 核心线程数量.
	 * @param maxPoolSize 最大线程数量.
	 * @param keepAliveSeconds 空闲秒数.
	 * @param queueSize 阻塞队列长度.
	 * @param handler 丢失策略.
	 * @return 线程池对象.
	 */
	public ThreadPoolExecutor getThreadPoolExecutor(final String threadNamePrefix, 
			final int corePoolSize, final int maxPoolSize, 
			final long keepAliveSeconds, final int queueSize,
			final RejectedExecutionHandler handler) {
		
		// 已有指定线程池,直接获取返回.
		if (threadPoolMap.get(threadNamePrefix) != null) {
			return threadPoolMap.get(threadNamePrefix);
		}
		
		synchronized (threadPoolMap) {
			// 二次检查.
			if (threadPoolMap.get(threadNamePrefix) != null) {
				return threadPoolMap.get(threadNamePrefix);
			}
			
			// 线程创建指定名称前缀.
			final ThreadFactory factory = 
					new ThreadFactoryBuilder().setNameFormat(threadNamePrefix + "%s").build();
			// 线程池.
			final ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, 
								keepAliveSeconds, TimeUnit.SECONDS, 
								new LinkedBlockingQueue<Runnable>(queueSize), 
								factory, 
								handler);
			addShutdownHook(executor);
			threadPoolMap.put(threadNamePrefix, executor);
		}
		
		return threadPoolMap.get(threadNamePrefix);
	}
	
	/**
	 * 添加关闭系统钩子.
	 */
	private void addShutdownHook(final ThreadPoolExecutor executor) {
		Runtime.getRuntime().addShutdownHook(new Thread(() -> {
			executor.shutdown();
			try {
				// 最长等待30秒,线程池所有任务执行.
				if (!executor.awaitTermination(30, TimeUnit.SECONDS)) {
					executor.shutdownNow();
				}
			} catch (Exception e) {
				executor.shutdownNow();
			}
		}));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值