线程池工具类

本文详细探讨了Java中的线程池工具类ThreadPoolUtils,包括其核心功能、使用场景和最佳实践。通过实例代码,展示了如何有效利用线程池提升程序并发性能和资源管理效率。
摘要由CSDN通过智能技术生成

线程池的工具类ThreadPoolUtils

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtils {
	// 核心线程数
	private static final int CORE_POOL_SIZE = 50;
	// 最大线程数
	private static final int MAX_POOL_ZISE = 1000;
	// 存活时间
	private static final Long KEEP_ALIVE_TIME = 60L;

	private ThreadPoolUtils() {
	}

	private static class ThreadPoolHolder {
		private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_ZISE,
			KEEP_ALIVE_TIME, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
	}

	public static ExecutorService getExecutorService() {
		return ThreadPoolHolder.executor;
	}

	public static void execute(Runnable task) {
		ThreadPoolHolder.executor.execute(getExcpThread(task));
	}

	public static void execute(Runnable runnable, Thread.UncaughtExceptionHandler excpHandler) {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值