import org.springframework.stereotype.Component;
import java.util.concurrent.*;
public class ThreadPoolsUtil {
public static ThreadPoolExecutor threadPool;
/**
* 无返回值直接执行, 管他娘的
* @param runnable
*/
public static void execute(Runnable runnable){
getThreadPool().execute(runnable);
}
/**
* 返回值直接执行, 管他娘的
* @param callable
*/
public static <T> Future<T> submit(Callable<T> callable){
return getThreadPool().submit(callable);
}
/**
* dcs获取线程池
* @return 线程池对象
*/
public static ThreadPoolExecutor getThreadPool() {
if (threadPool != null) {
return threadPool;
} else {
synchronized (ThreadPoolsUtil.class) {
if (threadPool == null) {
int size = Runtime.getRuntime().availableProcessors();
threadPool = new ThreadPoolExecutor(size, size*2, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
}
return threadPool;
}
}
}
}
网友评论