线程池


import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



/**
 * 线程池管理
 * 
 */
public class ThreadPoolManage {
	
	private static Log log = LogFactory.getLog(ThreadPoolManage.class);
	
	private static ThreadPoolManage THREAD_POOL_MANAGE = null;
	private int maxPoolSize = 1000000;//最大线程池数
	private int minPoolSize = 10;//最小线程池数
	private int maxQueueSize = 1000000;//最大线程池数
	private int minQueueSize = 10;//最小线程池数
    private int poolSize = 1000;// 默认线程池大小1000
    private int queueSize = 1000;//默认队列大小1000
    
    private long keepAliveTime = 5;//最大空闲时间(单位秒)
    private ThreadPoolExecutor threadPool = null;
    
    /**
     * 处理任务的优先级为: 
     * 核心线程corePoolSize、任务队列workQueue、最大线程maximumPoolSize,如果三者都满了,使用handler处理被拒绝的任务。 
     */
    private ThreadPoolManage() {
    	log.info("初始化##ThreadPoolManage##,线程池大小=" + poolSize + ",队列大小=" + queueSize);
    	//创建固定大小的线程数
        threadPool = (ThreadPoolExecutor)Executors.newFixedThreadPool(poolSize);
        threadPool.setKeepAliveTime(keepAliveTime, TimeUnit.SECONDS);
        //threadPool.setMaximumPoolSize(maxPoolSize);
        //handler处理被拒绝的任务策略:重试添加当前的任务,他会自动重复调用execute()方法 
        threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
    }
    private ThreadPoolManage(int threadPoolSize,int queueSize) {
    	if (threadPoolSize >= 0) {
    		if (threadPoolSize < maxPoolSize) {
    			this.poolSize = threadPoolSize;
    		} else {
    			this.poolSize = maxPoolSize;
    		}
    		
    	} else {
    		this.poolSize = minPoolSize;
    	}
    	if (queueSize >= 0) {
    		if (queueSize < maxQueueSize) {
    			this.queueSize = queueSize;
    		} else {
    			this.queueSize = maxQueueSize;
    		}
    		
    	} else {
    		this.queueSize = minQueueSize;
    	}
    	log.info("初始化##ThreadPoolManage##,线程池大小=" + poolSize + ",队列大小=" + this.queueSize);
    	//创建固定大小的线程数
        threadPool = (ThreadPoolExecutor)Executors.newFixedThreadPool(poolSize);
        threadPool.setKeepAliveTime(keepAliveTime, TimeUnit.SECONDS);
        threadPool.setMaximumPoolSize(maxPoolSize);
        //threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
    }
    /**
     * 
     * 获取默认的线程池管理实例
     * 
     * @return ThreadPoolManage
     */
    public synchronized static ThreadPoolManage getInstance() {
    	
    	if (THREAD_POOL_MANAGE == null) {
    		THREAD_POOL_MANAGE = new ThreadPoolManage();
    	}
        return THREAD_POOL_MANAGE;
    }
    /**
     * 
     * 获取指定固定大小的的线程池管理实例
     * 
     * @param threadPoolSize
     * @param queueSize
     * @return ThreadPoolManage
     */
    public synchronized static ThreadPoolManage getInstance(int threadPoolSize,int queueSize) {
    	if (THREAD_POOL_MANAGE == null) {
    		THREAD_POOL_MANAGE = new ThreadPoolManage(threadPoolSize,queueSize);
    	}
        return THREAD_POOL_MANAGE;
    }
    
    /**
     * 
     * 更新线程池和队列大小
     * 
     * @param threadPoolSize
     * @param queueSize void
     */
    public static void updateThreadPoolSize(int threadPoolSize,int queueSize) {
    	
    	if (THREAD_POOL_MANAGE != null 
    			&& THREAD_POOL_MANAGE.threadPool != null 
    			&& !THREAD_POOL_MANAGE.threadPool.isShutdown()) {
    		log.info("更新线程池大小##ThreadPoolManage##,线程池大小=" + threadPoolSize + ",队列大小=" + queueSize);
    		if (threadPoolSize > 0) {
    			if (threadPoolSize < THREAD_POOL_MANAGE.maxPoolSize) {
    				THREAD_POOL_MANAGE.threadPool.setCorePoolSize(threadPoolSize);
    			} else {
    				THREAD_POOL_MANAGE.threadPool.setCorePoolSize(THREAD_POOL_MANAGE.maxPoolSize);
    			}
    			
    		} else {
    			THREAD_POOL_MANAGE.threadPool.setCorePoolSize(THREAD_POOL_MANAGE.minPoolSize);
    		}
    		if (queueSize > 0) {
    			if (queueSize < THREAD_POOL_MANAGE.maxQueueSize) {
    				THREAD_POOL_MANAGE.queueSize = queueSize;
    			} else {
    				THREAD_POOL_MANAGE.queueSize = THREAD_POOL_MANAGE.maxQueueSize;
    			}
    		} else {
    			THREAD_POOL_MANAGE.queueSize = THREAD_POOL_MANAGE.minQueueSize;
    		}
    	}
    }
    
    /**
     * 当一个任务通过execute(Runnable)方法欲添加到线程池时: 
     * 如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。 
     * 如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。 
     * 如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maximumPoolSize,建新的线程来处理被添加的任务。 
     * 如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maximumPoolSize,那么通过 handler所指定的策略来处理此任务。
     * @param task void
     */
    public void addTask(Runnable task) {
        threadPool.execute(task);
    }
    
    public int getCorePoolSize() {
        return this.threadPool.getCorePoolSize();
    }
    
    public int getTaskQueueSize() {
        return this.threadPool.getQueue().size();
    }
    /**
     * 
     * 队列是否已达到指定的数量
     * 
     * @return boolean
     */
    public synchronized boolean isQueueFull() {
    	if (this.threadPool.getQueue().size() >= queueSize) {
    		return true;
    	}
    	return false;
    }
}


你可能感兴趣的:(java)