//线程池处理集合数据
private void runThread(List<Dto> Dtos){
int sum = Dtos.size();
int thread = threadNum;// 线程数
final int avg = sum / thread;// 每个线程执行数量
final int renum = sum%thread;// 余数
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < thread; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
final int count = i;
cachedThreadPool.execute(new Runnable() {
public void run() {
List<Dto> DtoList=new ArrayList<>();
Map<String, List<Dto>> jsAuthMap=new HashMap<>();
log.info("线程"+count+"执行开始");
long strattimes=System.currentTimeMillis();
if(count==thread-1){//最后一个线程加上余数
for (int j = 0; j < avg+renum; j++) {
int number = j + count * avg;// 数据编号
DtoList.add(Dtos.get(number));
jsAuthMap.put("thread"+count,DtoList);
}
}else{
for (int j = 0; j < avg; j++) {
int number = j + count * avg;// 数据编号
DtoList.add(Dtos.get(number));
jsAuthMap.put("thread"+count,DtoList);
}
}
long endtimes=System.currentTimeMillis();
log.info("线程"+count+"执行结束,用时:"+(endtimes-strattimes)+"ms");
}
});
}
}
多线程、线程池处理集合数据
于 2021-07-26 09:14:44 首次发布