package com.npnets.thread;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
/**
* 2017/04/20
* @author Mr.zhang
*
*/
public final class RequestHandlerExecutorPool {
private static Logger logger = Logger.getLogger("POST");
private static RequestHandlerExecutorPool threadPoolManager = new RequestHandlerExecutorPool();
private static final int SIZE_MAX_POOL = 50;
private static final int SIZE_MAX_QUEUE = 2000;
public static RequestHandlerExecutorPool newInstance() {
logger.info("threadPoolManager init 初始化完成");
return threadPoolManager;
}
/**************************************************************************************************************
* 常见的几种线程技术
**************************************************************************************************************
* Java通过Executors提供四种线程池,分别为:
* newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
* newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
* newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。 newSingleThreadExecutor
* 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
*
* 1、public static ExecutorService newFixedThreadPool(int nThreads) {
* return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
*
* 2、 public static ExecutorService newSingleThreadExecutor() {
* return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
*
* 3、public static ExecutorService newCachedThreadPool() {
* return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
****************************************************************************************************************/
/**
* 线程池
* @param corePoolSize - 池中所保存的线程数,包括空闲线程。
* @param maximumPoolSize - 池中允许的最大线程数。
* @param keepAliveTime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
* @param unit - keepAliveTime 参数的时间单位。
* @param workQueue - 执行前用于保持任务的队列。此队列仅由保持 execute 方法提交的 Runnable 任务。
* @param handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。
*/
private final ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), SIZE_MAX_POOL, 120L,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(SIZE_MAX_QUEUE));
private RequestHandlerExecutorPool() {
}
public void perpare() {
if (mThreadPool.isShutdown() && !mThreadPool.prestartCoreThread()) {
@SuppressWarnings("unused")
int startThread = mThreadPool.prestartAllCoreThreads();
}
}
public void addExecuteTask(Runnable task) {
if (task != null) {
mThreadPool.execute(task);
logger.info(" [ 当前线程池中线程数目为:"+getPoolSize()+" , 最大线程池为:"+getMaximumPoolSize()+" , 当前队列大小:" + getQueue() + " , 已处理完任务:" + getCompletedTaskCount() + " ] ");
}
}
protected boolean isTaskEnd() {
if (mThreadPool.getActiveCount() == 0) {
return true;
} else {
return false;
}
}
public int getQueue(){
return mThreadPool.getQueue().size();
}
public int getMaximumPoolSize(){
return mThreadPool.getMaximumPoolSize();
}
public int getPoolSize(){
return mThreadPool.getPoolSize();
}
public long getCompletedTaskCount(){
return mThreadPool.getCompletedTaskCount();
}
public void shutdown() {
mThreadPool.shutdown();
}
}