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

elephant.rpc.threadpool.ThreadPoolManager Maven / Gradle / Ivy

The newest version!
package elephant.rpc.threadpool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import elephant.utils.ObjectPrinter;

/**
 * 
 * @author skydu
 *
 */
public class ThreadPoolManager{
	//
	private static Logger logger=LoggerFactory.getLogger(ThreadPoolManager.class);
	//
	private static final int DEFAULT_CORE_POOL_SIZE=32;
	private static final int DEFAULT_MAX_POOL_SIZE=64;
	//
	private static final String WORKER_THREAD_POOL_NAME="WorkerThread";
	private static final String SCHEDULED_THREAD_POOL_NAME="ScheduledThread";
	//
	private ThreadPoolExecutor workerThreadPool;

	private ScheduledExecutorService scheduledThreadPool;
	
	private LinkedBlockingQueue requestQueue;
	//
	private int workerPoolSize;
	private int maxWorkerPoolSize;
	private int linkedBlockingQueueSize;
	private int scheduledPoolSize;
	private ClassLoader contextClassLoader;
	//
	public ThreadPoolManager(ClassLoader contextClassLoader) {
		workerPoolSize=DEFAULT_CORE_POOL_SIZE;
		maxWorkerPoolSize=DEFAULT_MAX_POOL_SIZE;
		linkedBlockingQueueSize=workerPoolSize*2;
		scheduledPoolSize=5;
		this.contextClassLoader=contextClassLoader;
	}
	//
	public void init() {
		if(logger.isInfoEnabled()){
			logger.info(getClass().getSimpleName()+" init");
		}
	}
	//
	public void start() {
		if(logger.isInfoEnabled()){
			logger.info(getClass().getSimpleName()+" start");
		}
		requestQueue=new LinkedBlockingQueue(linkedBlockingQueueSize);
		workerThreadPool=new ThreadPoolExecutor(
				workerPoolSize, maxWorkerPoolSize,
				60L,
				TimeUnit.SECONDS, 
				requestQueue,
				new RPCThreadFactory(WORKER_THREAD_POOL_NAME,contextClassLoader));
		workerThreadPool.setRejectedExecutionHandler(
				new ThreadPoolExecutor.CallerRunsPolicy());
		//
		scheduledThreadPool=new ScheduledThreadPoolExecutor(
				scheduledPoolSize,
				new RPCThreadFactory(SCHEDULED_THREAD_POOL_NAME,contextClassLoader),
				new ThreadPoolExecutor.AbortPolicy());
	}

	public void stop() {
		if(logger.isInfoEnabled()){
			logger.info(getClass().getSimpleName()+" stop");
		}
		if(workerThreadPool!=null){
			workerThreadPool.shutdown();
		}
	}
	//
	public void execute(Runnable worker){
		workerThreadPool.execute(new WorkerRunnable(worker));
	}
	//
	public ScheduledFuture scheduleAtFixedRate(Runnable command,
			long initialDelay, long period, TimeUnit unit) {
		return scheduledThreadPool.scheduleAtFixedRate(command,
				initialDelay, period, unit);
	}
	//
	public void setWorkerPoolSize(int workerPoolSize) {
		this.workerPoolSize = workerPoolSize;
	}
	//
	public void setMaxWorkerPoolSize(int maxWorkerPoolSize) {
		this.maxWorkerPoolSize = maxWorkerPoolSize;
	}
	//
	public void setScheduledPoolSize(int scheduledPoolSize) {
		this.scheduledPoolSize = scheduledPoolSize;
	}
	//
	/**
	 * @return the contextClassLoader
	 */
	public ClassLoader getContextClassLoader() {
		return contextClassLoader;
	}
	/**
	 * @param contextClassLoader the contextClassLoader to set
	 */
	public void setContextClassLoader(ClassLoader contextClassLoader) {
		this.contextClassLoader = contextClassLoader;
	}
	//
	public String dump() {
		ObjectPrinter op=new ObjectPrinter();
		op.section(getClass().getSimpleName());
		op.print("workerThreadName",WORKER_THREAD_POOL_NAME);
		op.print("workerPoolSize",workerPoolSize);
		op.print("maxWorkerPoolSize",maxWorkerPoolSize);
		op.print("BlockingQueueSize",linkedBlockingQueueSize);
		op.print("scheduledThreadName",SCHEDULED_THREAD_POOL_NAME);
		op.print("scheduledPoolSize",scheduledPoolSize);
		return op.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy