content: package org.bianqi.wangyi.executor;
  
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.atomic.AtomicInteger;
  import java.util.concurrent.locks.Condition;
  import java.util.concurrent.locks.ReentrantLock;
  
  /**
   *  线程池
   * <p>Title: ThreadPool</p>
   * <p>Description: </p>
   * <p>School: qiqihar university</p> 
   * @author	BQ
   * @date	2018年2月11日下午2:50:51
   * @version 1.0.0
   */
  public class ThreadPool {
  	
  	private int threadNum;
  
      private AtomicInteger threadAlive = new AtomicInteger();
  
      private ReentrantLock reentrantLock = new ReentrantLock();
  
      private Condition condition = reentrantLock.newCondition();
      
      private ExecutorService executorService;
      
      public ThreadPool(int threadNum) {
          this.threadNum = threadNum;
          this.executorService = Executors.newFixedThreadPool(threadNum);
      }
  
      public ThreadPool(int threadNum, ExecutorService executorService) {
          this.threadNum = threadNum;
          this.executorService = executorService;
      }
      
      public void execute(final Runnable runnable) {
  
          if (threadAlive.get() >= threadNum) {
              try {
                  reentrantLock.lock();
                  while (threadAlive.get() >= threadNum) {
                      try {
                          condition.await();
                      } catch (InterruptedException e) {
                      }
                  }
              } finally {
                  reentrantLock.unlock();
              }
          }
          threadAlive.incrementAndGet();
          executorService.execute(new Runnable() {
              @Override
              public void run() {
                  try {
                      runnable.run();
                  } finally {
                      try {
                          reentrantLock.lock();
                          threadAlive.decrementAndGet();
                          condition.signal();
                      } finally {
                          reentrantLock.unlock();
                      }
                  }
              }
          });
      }
  
      public boolean isShutdown() {
          return executorService.isShutdown();
      }
  
      public void shutdown() {
          executorService.shutdown();
      }
  
  	public int getThreadNum() {
  		return threadNum;
  	}
  
  	public void setThreadNum(int threadNum) {
  		this.threadNum = threadNum;
  	}
  
  	public AtomicInteger getThreadAlive() {
  		return threadAlive;
  	}
  
  	public void setThreadAlive(AtomicInteger threadAlive) {
  		this.threadAlive = threadAlive;
  	}
      
  }
  