MinerThreadPool.java 线程池
package com.iteye.injavawetrust.miner;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池
* @author InJavaWeTrust
*
*/
public class MinerThreadPool {
private static volatile ThreadPoolExecutor executor = null;
/**
* 核心池的大小
*/
private static int corePoolSize = 100;
/**
* 线程池最大线程数
*/
private static int maximumPoolSize = 200;
/**
* 线程没有任务执行时最多保持多久时间会终止
*/
private static long keepAliveTime = 100L;
/**
* 参数keepAliveTime的时间单位
*/
private static TimeUnit unit = TimeUnit.SECONDS;
private MinerThreadPool() {
}
public static ThreadPoolExecutor getInstance() {
if (null == executor) {
executor = getInstance(corePoolSize, maximumPoolSize, keepAliveTime, unit);
}
return executor;
}
public static ThreadPoolExecutor getInstance(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit){
if (null == executor) {
synchronized (MinerThreadPool.class) {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, queue);
}
}
return executor;
}
/**
* shutdown
*/
public static void shutdown() {
if (executor != null) {
executor.shutdown();
}
}
}
返回列表