package org.voovan.tools.threadpool;

import org.voovan.Global;
import org.voovan.tools.TProperties;

import java.util.Timer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池
 *
 * @author helyho
 *
 * Voovan Framework.
 * WebSite: https://github.com/helyho/Voovan
 * Licence: Apache v2 License
 */
public class ThreadPool {
	private final static int cpuCoreCount = Runtime.getRuntime().availableProcessors();
	protected static int MIN_POOL_SIZE = 2*cpuCoreCount;
	protected static int MAX_POOL_SIZE = 100*cpuCoreCount;

	/**
	 * 获取线程池最小活动线程数
	 * @return 线程池最小活动线程数
	 */
	public static int getMinPoolSize() {
		int minPoolTimes = TProperties.getInt(Global.getFrameworkConfigFile(), "ThreadPoolMinSize");
		MIN_POOL_SIZE = (minPoolTimes == 0 ? 2 : minPoolTimes) * cpuCoreCount;
		return MIN_POOL_SIZE;
	}

	/**
	 * 获取线程池最大活动线程数
	 * @return 线程池最大活动线程数
	 */
	public static int getMaxPoolSize() {
		int maxPoolTimes = TProperties.getInt(Global.getFrameworkConfigFile(), "ThreadPoolMaxSize");
		MIN_POOL_SIZE = (maxPoolTimes == 0 ? 100 : maxPoolTimes) * cpuCoreCount;
		return MAX_POOL_SIZE;
	}


	private ThreadPool(){
	}

	private static ThreadPoolExecutor createThreadPool(){
		ThreadPoolExecutor threadPoolInstance = new ThreadPoolExecutor(getMinPoolSize(), getMaxPoolSize(), 1, TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(cpuCoreCount*500));
		//设置allowCoreThreadTimeOut,允许回收超时的线程
		threadPoolInstance.allowCoreThreadTimeOut(true);

		Timer timer = new Timer("VOOVAN@THREAD_POOL_TIMER");
		ThreadPoolTask threadPoolTask = new ThreadPoolTask(threadPoolInstance);
		timer.schedule(threadPoolTask, 1, 1000);

		return threadPoolInstance;
	}

	private static ThreadPoolExecutor createThreadPool(int corePoolSize, int maxPoolSize){
		ThreadPoolExecutor threadPoolInstance = new ThreadPoolExecutor(getMinPoolSize(), getMaxPoolSize(), 1, TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(cpuCoreCount*500));
		//设置allowCoreThreadTimeOut,允许回收超时的线程
		threadPoolInstance.allowCoreThreadTimeOut(true);

		Timer timer = new Timer("VOOVAN@THREAD_POOL_TIMER");
		ThreadPoolTask threadPoolTask = new ThreadPoolTask(threadPoolInstance);
		timer.schedule(threadPoolTask, 1, 1000);

		return threadPoolInstance;
	}

	public static ThreadPoolExecutor getNewThreadPool(){
		return createThreadPool();
	}
}

@@ -1073,34 +1073,34 @@
 olMaxSize%22);%0A%09%09M
-IN
+AX
 _POOL_SIZE = (ma
@@ -1298,36 +1298,38 @@
 dPoolInstance = 
-new 
+create
 ThreadPoolExecut
@@ -1318,32 +1318,24 @@
 teThreadPool
-Executor
 (getMinPoolS
@@ -1364,170 +1364,33 @@
 ), 1
-, TimeUnit.MINUTES,new ArrayBlockingQueue%3CRunnable%3E(cpuCoreCount*500));%0A%09%09//%E8%AE%BE%E7%BD%AEallowCoreThreadTimeOut,%E5%85%81%E8%AE%B8%E5%9B%9E%E6%94%B6%E8%B6%85%E6%97%B6%E7%9A%84%E7%BA%BF%E7%A8%8B%0A%09%09threadPoolInstance.allowCoreThreadTimeOut(true);%0A
+000*60);%0A%0A%09%09//%E5%90%AF%E5%8A%A8%E7%BA%BF%E7%A8%8B%E6%B1%A0%E8%87%AA%E5%8A%A8%E8%B0%83%E6%95%B4%E4%BB%BB%E5%8A%A1
 %0A%09%09T
@@ -1550,33 +1550,32 @@
 Task, 1, 1000);%0A
-%0A
 %09%09return threadP
@@ -1588,31 +1588,160 @@
 tance;%0A%09%7D%0A%0A%09
-private
+/**%0A%09 * %E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B%E6%B1%A0%0A%09 * @param mimPoolSize %E6%9C%80%E5%B0%8F%E7%BA%BF%E7%A8%8B%E6%95%B0%0A%09 * @param maxPoolSize %E6%9C%80%E5%A4%A7%E7%BA%BF%E7%A8%8B%E6%95%B0%0A%09 * @param threadTimeout %E7%BA%BF%E7%A8%8B%E9%97%B2%E7%BD%AE%E8%B6%85%E6%97%B6%E6%97%B6%E9%97%B4%0A%09 * @return %E7%BA%BF%E7%A8%8B%E6%B1%A0%E5%AF%B9%E8%B1%A1%0A%09 */%0A%09public
  static Thre
@@ -1780,12 +1780,11 @@
 int 
-core
+mim
 Pool
@@ -1804,16 +1804,35 @@
 PoolSize
+, int threadTimeout
 )%7B%0A%09%09Thr
@@ -1891,30 +1891,27 @@
 xecutor(
-getMin
+mim
 PoolSize
 (), getM
@@ -1898,32 +1898,27 @@
 (mimPoolSize
-(), getM
+, m
 axPoolSize()
@@ -1915,21 +1915,31 @@
 PoolSize
-(), 1
+, threadTimeout
 , TimeUn
@@ -1947,14 +1947,20 @@
 t.MI
-NUTE
+LLISECOND
 S,
+ 
 new 
@@ -2103,181 +2103,8 @@
 );%0A%0A
-%09%09Timer timer = new Timer(%22VOOVAN@THREAD_POOL_TIMER%22);%0A%09%09ThreadPoolTask threadPoolTask = new ThreadPoolTask(threadPoolInstance);%0A%09%09timer.schedule(threadPoolTask, 1, 1000);%0A%0A
 %09%09re

修复线程池自动调整的 bug