线程池工具类

2018-02-12 10:50:04来源:oschina作者:steveJox人点击

分享
/**
* @ClassName:SimpleThreadPoolUtil
* @Desc: simple 线程池工具类
**/
public class SimpleThreadPoolUtil {
private static final Map POOLS = new HashMap<>();
public enum PoolType{
TEST_THREAD_POOL
}
static {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("threadFacotry-%d").build();
ExecutorService pool = new ThreadPoolExecutor(5, 5,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
POOLS.put(PoolType.TEST_THREAD_POOL,pool);
}
/**
* MethodName: getThreadPool
* param: type 线程池类型
* return:
* describe: 获取线程池
**/
public static ExecutorService getThreadPool(PoolType type){
return POOLS.get(type);
}/**
* MethodName: submit
* param: task 异步任务,type 线程池类型
* return:futuretask
* describe: 根据指定类型线程池运行一个异步任务
**/
public static Future submit(Callable task,PoolType type){
ExecutorService pool = POOLS.get(type);
if (pool == null || pool.isShutdown()){
throw new IllegalArgumentException("pool not exists or had been shut down type:"+type);
}
returnpool.submit(task);
}
}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台