/**
* 线程池使用
* @author mamy
*
*/
public class DispatchIntfSmoImpl implements IDispatchIntfSmo {

/\*\*  

* 多线程执行任务
* @param archiveReqVo
* @return
* @throws Exception
*/ public Map<String,String> threadBusiInterface(ArchiveReqVo archiveReqVo) throws Exception {

    //1.获取线程池实例  
    ThreadPoolExecutor executor = null;  
    Map<String,String> map=new HashMap();    
    try {  
        executor = SoThreadPoolFactory.getInstance();  
        FutureTask<Map<String,String>> futureTask=new FutureTask<>(new DispatchBusi(archiveReqVo));  
        executor.execute(futureTask);  
        map.put("resultCode","0");  
        map.put("resultMsg","处理成功!");  
    } catch (Exception e) {  
        map.put("resultCode","-1");  
        map.put("resultMsg","处理失败!");  
        log.debug("初始化获取线程池实例发生异常:", e);  
    }  
    return map;  
}  

}

/**
* 线程池类
* @author mamy
*
*/
public class SoThreadPoolFactory {
//任务线程队列
//线程池维护线程的最小数量
private static int CORE_POOL_SIZE = 8;

//线程池维护线程的最大数量
private static int MAX_POOL_SIZE = 16;

//线程池维护线程所允许的空闲时间(单位秒)
private static long KEEP_ALIVE_TIME = 600;

//线程池维护线程所允许的空闲时间单元
private static TimeUnit TIME_UNIT = TimeUnit.SECONDS;

//有界队列最大长度
private static int MAX_QUEUE_SIZE = 1000;

//有界队列,当使用有限的 MAX_POOL_SIZE 时,有界队列(如 ArrayBlockingQueue)有助于防止资源耗尽,但是可能较难调整和控制。
private static ArrayBlockingQueue<Runnable> queue = null;

private static PropertiesUtil propertiesUtil=new PropertiesUtil("intf-listener.properties");

static
{

  // 获取配置信息  

CORE_POOL_SIZE = Integer.parseInt(propertiesUtil.readProperty("threadPool.CORE_POOL_SIZE"));

  MAX\_POOL\_SIZE \= Integer.parseInt(propertiesUtil.readProperty("threadPool.MAX\_POOL\_SIZE"));  
  KEEP\_ALIVE\_TIME \= Integer.parseInt(propertiesUtil.readProperty("threadPool.KEEP\_ALIVE\_TIME"));  
  TIME\_UNIT \= TimeUnit.valueOf(propertiesUtil.readProperty("threadPool.TIME\_UNIT"));  
  MAX\_QUEUE\_SIZE \= Integer.parseInt(propertiesUtil.readProperty("threadPool.MAX\_QUEUE\_SIZE"));;  
  queue \= new ArrayBlockingQueue<Runnable>(MAX\_QUEUE\_SIZE);  

  queue \= new ArrayBlockingQueue<Runnable>(MAX\_QUEUE\_SIZE);  
  log.debug("校验性规则线程池维护线程的最小数量:{}", CORE\_POOL\_SIZE);  
  log.debug("校验性规则线程池维护线程的最大数量:{}", MAX\_POOL\_SIZE);  
  log.debug("校验性规则线程池维护线程所允许的空闲时间单元:{}", TIME\_UNIT.name());  
  log.debug("校验性规则线程池维护线程所允许的空闲时间:{}", KEEP\_ALIVE\_TIME);  
  log.debug("校验性规则有界队列最大长度:{}", MAX\_QUEUE\_SIZE);  

}

private static class ThreadPoolExecutorInstance {

  // ThreadPoolExecutor.CallerRunsPolicy 表示拒绝任务,并在调用者的线程中直接执行该任务  

private static final ThreadPoolExecutor INSTANCE = new ThreadPoolExecutor(

        CORE\_POOL\_SIZE, MAX\_POOL\_SIZE, KEEP\_ALIVE\_TIME, TIME\_UNIT,  
        queue, new ThreadPoolExecutor.CallerRunsPolicy());  

}

public static ThreadPoolExecutor getInstance() {

  log.debug("线程池维护线程的最小数量:{}", ThreadPoolExecutorInstance.INSTANCE.getCorePoolSize());  
  log.debug("线程中线程池大小:{}", ThreadPoolExecutorInstance.INSTANCE.getPoolSize());  
  log.debug("线程池维护线程的最大数量:{}", ThreadPoolExecutorInstance.INSTANCE.getMaximumPoolSize());  
  log.debug("线程池当前活跃线程数:{}", ThreadPoolExecutorInstance.INSTANCE.getActiveCount());  
  log.debug("线程池维护线程所允许的空闲时间:{}", ThreadPoolExecutorInstance.INSTANCE.getKeepAliveTime(TIME\_UNIT));  
  log.debug("规则有界队列最大长度:{}", ThreadPoolExecutorInstance.INSTANCE.getQueue().size());  
  log.debug("规则线程中的任务数:{}", ThreadPoolExecutorInstance.INSTANCE.getTaskCount());  
  log.debug("规则线程中的完成任务数:{}", ThreadPoolExecutorInstance.INSTANCE.getCompletedTaskCount());  
  return ThreadPoolExecutorInstance.INSTANCE;  

}
}


begin_again
4 声望0 粉丝

文章都是来自实际项目100%没问题的,大家可以放心食用。有好的改进可以私信给我,我更新,希望大家共同维护。