线程池管理类和线程池类

一.线程池管理类

/**
 * 线程池管理类
 */
public class ThreadPoolFactory
{
   
    private static final Log logger = LogFactory.getLog(ThreadPoolFactory.class);
      
    /**
     * 放入管理线程池线程间隔时间
     */
    private final static int PUT_DEFAULTPOOL_INTERVAL = 3;
    
    /**
     * 线程池保持对象
     */
    private static Map<String, ThreadPool> poolMap = new HashMap<String, ThreadPool>();
    
    /**
     * 请求Request缓冲队列(缓存队列满时)
     */
    public static final Queue<Runnable> msgQueue = new ConcurrentLinkedQueue<Runnable>();
    
    static
    {
        // 访问请求Request缓存的调度线程
        final Runnable accessBufferThread = new Runnable()
        {
            public void run()
            {
                // 查看是否有待定请求,如果有,则添加到线程池中
                if (!msgQueue.isEmpty())
                {
                    Runnable r = msgQueue.poll();
                    
                    logger.error("缓存队列线程放入默认线程池执行" + r);
                    
                    getThreadPool("defaultThreadPool").execute(r);
                }
            }
        };
        
        // 调度线程池
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        
        // 定时调度
        scheduler.scheduleAtFixedRate(accessBufferThread, 0, PUT_DEFAULTPOOL_INTERVAL, TimeUnit.SECONDS);
    }
    
    /**
     * 根据key取得对应线程池实例
     * 
     */
    public static ThreadPool getThreadPool(String key)
    {
        ThreadPool obj = null;
        
        if (StringUtils.isNotBlank(key))
        {
            obj = poolMap.get(key);
            
            if (obj == null)
            {
                synchronized (ThreadPoolFactory.class)
                {
                    if (obj == null)
                    {
                        logger.error("线程池" + key + "不存在,已被创建");
                        
                        obj = new ThreadPool(key);
                        
                        poolMap.put(key, obj);
                    }
                }
            }
            else
            {
                logger.info("线程池" + key + "存在,已返回");
            }
            
        }
        
        return obj;
    }
    
    /**
     * 静态工厂不允许被实例化
     */
    private ThreadPoolFactory()
    {
    }
}

二.线程池类

/**
 * 线程池类
 */
public class ThreadPool
{
    /**
     * 日志组件
     */
    private final Log logger = LogFactory.getLog(ThreadPool.class);
    
    /**
     * 线程池名字
     */
    private String name;

    /**
     * 线程池维护线程的最少数量
     */
    private final static int CORE_POOL_SIZE = 5;
    
    /**
     * 线程池维护线程的最大数量
     */
    private final static int MAX_POOL_SIZE = 100;
    
    /**
     * 线程池维护线程所允许的空闲时间
     */
    private final static int KEEP_ALIVE_TIME = 180;
    
    /**
     * 线程池所使用的缓冲队列大小
     */
    private final static int WORK_QUEUE_SIZE = 5000;
    
    /**
     * handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序
     */
    final RejectedExecutionHandler handler = new RejectedExecutionHandler()
    {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor)
        {
            logger.error(name + "已满, 线程被放入公共队列中等待执行 " + r);
            
            boolean result = ThreadPoolFactory.msgQueue.offer(r);
            
            if (!result)
            {
                logger.error("放入等待队列失败,系统发生严重错误");
            }
        }
    };
    
    /**
     * 线程池
     */
    private ThreadPoolExecutor threadPool;
    
    /**
     * 构造方法
     */
    ThreadPool(String name)
    {
        
        logger.error("CORE_POOL_SIZE=" + CORE_POOL_SIZE + ", MAX_POOL_SIZE=" + MAX_POOL_SIZE + ", KEEP_ALIVE_TIME="
                + KEEP_ALIVE_TIME + ", WORK_QUEUE_SIZE=" + WORK_QUEUE_SIZE);
        
        this.name = name;
        
        threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(WORK_QUEUE_SIZE), this.handler);
    }
    
    public void execute(Runnable task)
    {
        threadPool.execute(task);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章