开发过程中我们经常有需用用到线程池的场景,防止服务器资源不足导致OOM,所以就需要用到线程池来做资源的及时的回收释放,来做到资源利用的最大化,下面就是我自己针对EXECUTOR框架的一个线程池封装,希望可以帮到同行小伙伴。public class ThreadPool {

private static ThreadPool pool;
private static final int CPU_COUNT = 2;
private static boolean init = false;
private final int corePoolSize;
private final int maxPoolSize;
private ThreadPoolExecutor newExecutor;
private static final int DEFAULT_NEW_POOL_SIZE = Runtime.getRuntime().availableProcessors() * CPU_COUNT;
private static final int DEFAULT_NEW_MAX_POOL_SIZE = 200;
private static final int DEFAULT_NEW_KEEP_ALIVE_TIME = 3000;
private static final int DEFAULT_NEW_QUEUE_SIZE = 1000;

public ThreadPool() {
	this.corePoolSize = DEFAULT_NEW_POOL_SIZE;
	this.maxPoolSize = DEFAULT_NEW_MAX_POOL_SIZE;
	newExecutor = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize, DEFAULT_NEW_KEEP_ALIVE_TIME,
			TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(DEFAULT_NEW_QUEUE_SIZE),
			new ThreadPoolExecutor.DiscardOldestPolicy());
}

public ThreadPool(int corePoolSize) {
	this.corePoolSize = corePoolSize;
	this.maxPoolSize = DEFAULT_NEW_MAX_POOL_SIZE;
	newExecutor = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize, DEFAULT_NEW_KEEP_ALIVE_TIME,
			TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(DEFAULT_NEW_QUEUE_SIZE),
			new ThreadPoolExecutor.DiscardOldestPolicy());
}

/**
 * 初始化一个线程池
 */
public synchronized static void init() {
	if (!init) {
		pool = new ThreadPool();
		init = true;
	}
}

/**
 * 初始化指定大小的线程池
 *
 * @param corePoolSize
 *            线程池大小
 */
public synchronized static void init(int corePoolSize) {
	if (!init) {
		pool = new ThreadPool(corePoolSize);
		init = true;
	}
}

public static int getExecutorActiveCount() {
	return pool.newExecutor.getActiveCount();
}

/**
 * 启动一次静态线程池的顺序关闭,执行历史任务,但不接受新任务
 */
public synchronized static void shutdownStaticPool() {
	if (!pool.newExecutor.isShutdown())
		pool.newExecutor.shutdown();
}

/**
 * 如果线程池关闭后所有任务都已完成,返回true
 *
 * @return 是否关闭
 */
public static boolean isStaticPoolShutDown() {
	return pool.newExecutor.isShutdown();
}

/**
 * 加入一个任务到池内
 *
 * @param r
 *            线程任务
 * @return future对象
 */
public static Future<?> submit(Runnable r) {
	if (!init) {
		init();
	}
	return pool.newExecutor.submit(r);
}

/**
 * 回调线程加入一个任务到池内
 *
 * @param r
 *            线程任务
 * @return Future
 */
public static Future<?> submit(Callable<?> r) {
	if (!init) {
		init();
	}
	return pool.newExecutor.submit(r);
}

/**
 * 加入一组任务到池
 *
 * @param al
 *            批量任务
 * @return List
 */
public static List<Future<?>> submit(List<Runnable> al) {
	List<Future<?>> fl = new CopyOnWriteArrayList<Future<?>>();
	if (!init) {
		init();
	}
	for (Runnable r : al) {
		fl.add(submit(r));
	}
	return fl;
}

/**
 * 获取线程池的实例
 *
 * @return ThreadPoolExecutor
 */
public ThreadPoolExecutor getThreadPoolExecutor() {
	return pool.newExecutor;
}

/**
 * 添加任务
 *
 * @param r
 *            线程任务
 */
public void enqueue(Runnable r) {
	newExecutor.execute(r);
}

/**
 * 批量添加任务
 *
 * @param al
 *            批量任务
 */
public void enqueue(List<Runnable> al) {
	for (Runnable r : al) {
		enqueue(r);
	}
}

/**
 * 获取线程池中任务个数
 *
 * @return int
 */
public int getQueueCount() {
	return newExecutor.getQueue().size();
}

/**
 * 获得当前活动的线程数
 *
 * @return int
 */
public int getActiveCount() {
	return ((ThreadPoolExecutor) newExecutor).getActiveCount();
}

/**
 * 获得线程池中空闲线程的个数,近似值
 *
 * @return int
 */
public int getIdleThreadCount() {
	int getIdleThreadCount = this.corePoolSize - newExecutor.getActiveCount();
	return getIdleThreadCount > 0 ? getIdleThreadCount : 0;
}

/**
 * 获得线程池实例
 *
 * @return ThreadPool
 */
public static ThreadPool getPool() {
	return pool;
}

}