java线程池管理

  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. import java.util.concurrent.ArrayBlockingQueue;  
  4. import java.util.concurrent.Executors;  
  5. import java.util.concurrent.RejectedExecutionHandler;  
  6. import java.util.concurrent.ScheduledExecutorService;  
  7. import java.util.concurrent.ScheduledFuture;  
  8. import java.util.concurrent.ThreadPoolExecutor;  
  9. import java.util.concurrent.TimeUnit;  
  10.   
  11. /** 
  12.  * 线程池管理 
  13.  */  
  14. public class ThreadPoolManager {  
  15.    
  16.  private static ThreadPoolManager tpm = new ThreadPoolManager();  
  17.   
  18.  // 线程池维护线程的最少数量  
  19.  private final static int CORE_POOL_SIZE = 3;  
  20.   
  21.  // 线程池维护线程的最大数量  
  22.  private final static int MAX_POOL_SIZE = 10;  
  23.   
  24.  // 线程池维护线程所允许的空闲时间  
  25.  private final static int KEEP_ALIVE_TIME = 0;  
  26.   
  27.  // 线程池所使用的缓冲队列大小  
  28.  private final static int WORK_QUEUE_SIZE = 10;  
  29.   
  30.  // 任务调度周期  
  31.  private final static int TASK_QOS_PERIOD = 10;  
  32.   
  33.  // 任务缓冲队列  
  34.  private Queue<Runnable> taskQueue = new LinkedList<Runnable>();  
  35.   
  36.  /* 
  37.   * 线程池超出界线时将任务加入缓冲队列 
  38.   */  
  39.  final RejectedExecutionHandler handler = new RejectedExecutionHandler() {  
  40.   public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {  
  41.    taskQueue.offer(task);  
  42.   }  
  43.  };  
  44.   
  45.  /* 
  46.   * 将缓冲队列中的任务重新加载到线程池 
  47.   */  
  48.  final Runnable accessBufferThread = new Runnable() {  
  49.   public void run() {  
  50.    if (hasMoreAcquire()) {  
  51.     threadPool.execute(taskQueue.poll());  
  52.    }  
  53.   }  
  54.  };  
  55.   
  56.  /* 
  57.   * 创建一个调度线程池 
  58.   */  
  59.  final ScheduledExecutorService scheduler = Executors  
  60.    .newScheduledThreadPool(1);  
  61.   
  62.  /* 
  63.   * 通过调度线程周期性的执行缓冲队列中任务 
  64.   */  
  65.  final ScheduledFuture<?> taskHandler = scheduler.scheduleAtFixedRate(  
  66.    accessBufferThread, 0, TASK_QOS_PERIOD, TimeUnit.MILLISECONDS);  
  67.   
  68.  /* 
  69.   * 线程池 
  70.   */  
  71.  final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(  
  72.    CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,  
  73.    new ArrayBlockingQueue<Runnable>(WORK_QUEUE_SIZE), this.handler);  
  74.   
  75.  /* 
  76.   * 将构造方法访问修饰符设为私有,禁止任意实例化。 
  77.   */  
  78.  private ThreadPoolManager() {  
  79.   
  80.  }  
  81.   
  82.  /* 
  83.   * 线程池单例创建方法 
  84.   */  
  85.  public static ThreadPoolManager newInstance() {  
  86.   return tpm;  
  87.  }  
  88.   
  89.  /* 
  90.   * 消息队列检查方法 
  91.   */  
  92.  private boolean hasMoreAcquire() {  
  93.   return !taskQueue.isEmpty();  
  94.  }  
  95.   
  96.  /* 
  97.   * 向线程池中添加任务方法 
  98.   */  
  99.  public void addExecuteTask(Runnable task) {  
  100.   if (task != null) {  
  101.    threadPool.execute(task);  
  102.   }  
  103.  }  
  104. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值