package com.ylcm.sys.common;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池
 * @author Jump
 *
 */
public class MyThreadPool {
  
  private ExecutorService executorService;
  private static int poolSize = (Runtime.getRuntime().availableProcessors() << 1) + 10;
  
  private static MyThreadPool pool = new MyThreadPool();
  
  private MyThreadPool(){
    executorService = Executors.newFixedThreadPool(poolSize);
  }

  public static MyThreadPool getInstance(){
    return pool;
  }
  
  public void execute(Runnable cmd){
    executorService.execute(cmd);
  }
  
  public void shutdown(){
    executorService.shutdown();
  }
  
  public boolean isTerminated(){
    return isTerminated(1);
  }
  
  /**
   * 
   * @param intval sleep间隔秒数
   * @return boolean
   */
  public boolean isTerminated(int intval){
    while(!executorService.isTerminated()){
      try {
        Thread.sleep(1000*intval);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return true;
  }
  
  public boolean isShutdown(){
    return executorService.isShutdown();
  }
}

