自定义的ExcutorManager

/**
 * Copyright description
 */
package transfer;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
//import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * To Add descript here
 *
 * @author lixm
 * @since 2011-10-18
 */
public class ExcutorManager {
	public ThreadPoolExecutor executor;
	private int maximumPoolSize = 105;
	private long keepAliveTime = 600*1000;
	private static ExcutorManager instance;
	
	public int getMaximumPoolSize() {
		return maximumPoolSize;
	}
	public long getKeepAliveTime() {
		return keepAliveTime;
	}
	public ThreadPoolExecutor getExecutor() {
		return executor;
	}
	public void doStart(){
		int numCpu = Runtime.getRuntime().availableProcessors();
		int corePoolSize = numCpu+1;
		int maximumPoolSize = getMaximumPoolSize();
		long keepAliveTime = getKeepAliveTime();
		TimeUnit unit = TimeUnit.SECONDS;
		BlockingQueue workQueue = new SynchronousQueue();
		ThreadFactory threadFacotry = new ThreadFactory(){
			private AtomicInteger accptorNum = new AtomicInteger(0);
			@Override
			public Thread newThread(Runnable r) {
				String currentGroupName = Thread.currentThread().
						getThreadGroup().getName();
				String name = new StringBuilder(currentGroupName).append
						(".Processor").append
						(accptorNum.getAndIncrement()).toString();
				System.out.println("name:["+name+"]");
				return new Thread(Thread.currentThread().getThreadGroup(),r,name);
			}
			
		};
		executor = new ThreadPoolExecutor(2,2,
				keepAliveTime,unit,workQueue,
				threadFacotry);
	}
	
	public void doStop(){
		executor.shutdown();
		executor = null;
	}
	
	public  static void  executor(Runnable r){
		if(instance == null){
			instance = new ExcutorManager();
			instance.doStart();
		}
		instance.getExecutor().execute(r);
	}
	
	public static void shutdown(){
		instance.doStop();
	}
	
}

你可能感兴趣的:(manager)