org.voovan.tools.threadpool.ThreadPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of voovan-common Show documentation
Show all versions of voovan-common Show documentation
Voovan is a java framwork and it not depends on any third-party framework.
package org.voovan.tools.threadpool;
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 ThreadPool(){
}
private static ThreadPoolExecutor createThreadPool(){
int cpuCoreCount = Runtime.getRuntime().availableProcessors();
if(cpuCoreCount==0){
cpuCoreCount = 1;
}
ThreadPoolExecutor threadPoolInstance = new ThreadPoolExecutor(cpuCoreCount*2, cpuCoreCount*11, 1, TimeUnit.MINUTES,new ArrayBlockingQueue(cpuCoreCount*500));
//设置allowCoreThreadTimeOut,允许回收超时的线程
threadPoolInstance.allowCoreThreadTimeOut(true);
Timer timer = new Timer("VOOVAN@THREAD_POOL_TIMER");
ThreadPoolTask threadPoolTask = new ThreadPoolTask(threadPoolInstance,timer);
timer.schedule(threadPoolTask, 1, 1000);
return threadPoolInstance;
}
public static ThreadPoolExecutor getNewThreadPool(){
return createThreadPool();
}
}