All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.voovan.tools.threadpool.ThreadPool Maven / Gradle / Ivy

There is a newer version: 4.3.8
Show newest version
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();	
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy