package com.qing.core.utils;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
*
* 线程池工具类.
*
*/
public class ThreadPoolUtil {
private static final int DEFAULT_BLOCK_QUEUE_SIZE = 1024;
private static ThreadPoolExecutor threadPoolExecutor;
private ThreadPoolUtil() {
}
/**
* 获取处理器数目
*/
private static int availableProcessors = Runtime.getRuntime().availableProcessors();
/**
* 获取线程池
*
* @return
*/
private static ThreadPoolExecutor getPool() {
if (null != threadPoolExecutor) {
return threadPoolExecutor;
} else {
synchronized (ThreadPoolUtil.class) {
if (null == threadPoolExecutor) {
threadPoolExecutor = new ThreadPoolExecutor(availableProcessors * 2, availableProcessors * 4 + 1,
0L, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(DEFAULT_BLOCK_QUEUE_SIZE),
new ThreadPoolExecutor.CallerRunsPolicy());
}
return threadPoolExecutor;
}
}
}
/**
* 无返回值直接执行
*
* @param runnable
*/
public static void execute(Runnable runnable) {
getPool().execute(runnable);
}
/**
* 返回值直接执行
*
* @param callable
*/
public static <T> Future<T> submit(Callable<T> callable) {
return getPool().submit(callable);
}
}
ThreadPoolUtil.execute(new Runnable() {
@Override
public void run() {
}
});