public class ThreadPoolManager {
//线程执行器
private static ExecutorService executorService = null;
//设置线程个数
private static int nThreads = 5;
private static ThreadPoolManager taskExecutorPool = null;
//初始化线程池
static {
taskExecutorPool = new ThreadPoolManager(nThreads*getNumCores());
}
public ThreadPoolManager(int thread) {
executorService = Executors.newScheduledThreadPool(thread);
}
//取得单例
public static ThreadPoolManager getInstance(){
return taskExecutorPool;
}
//取得线程执行器
public static ExecutorService getExecutorService(){
return executorService;
}
public ScheduledExecutorService getScheduledExcutorService(){
return (ScheduledExecutorService)executorService;
}
public static int getNumCores() {
int threadCount = Runtime.getRuntime().availableProcessors();
return threadCount;
}
}
创建线程池以管理线程
最新推荐文章于 2022-08-16 19:35:22 发布