线程池
1定义单例设计模式
public class ThreadPoolManager {
private ExecutorService service;
private ThreadPoolManager(){
int num = Runtime.getRuntime().availableProcessors(); //获取当前系统的CPU数目
service = Executors.newFixedThreadPool(num*2);//ExecutorService 通常根据系统资源情况灵活定义线程池大小。
}
private static final ThreadPoolManager manager= new ThreadPoolManager();
public static ThreadPoolManager getInstance(){
return manager;
}
public void addTask(Runnable runnable){
service.execute(runnable);
}
}
2 调用
private ThreadPoolManager threadPoolManager;
threadPoolManager = ThreadPoolManager.getInstance();
3 生成
BaseTask taskThread = new BaseTask() //线程类--获取数据
this.threadPoolManager.addTask(taskThread);//将新建的现成添加入线程池