由于mysql承载的数据量有限所以这个表是按照月份进行水平分表
第一步 通过传入的起始年月切割成按照月份的集合然后再通过偏移量来把集合切分成5个集合List<List> codeList = Utils.averageAssign(yearMonthParam, poolSize);
第二步 初始化线程池 初始化计数器
ExecutorService pool = Executors.newFixedThreadPool(poolSize);
CountDownLatch latch = new CountDownLatch(poolSize);
第三步 循环外层集合,因为数据量大分5次获取,外层集合中开始线程池然后循环获取每个月数据
第四步 暂停当前线程,死循环 判断线程数是否结束
public List<ReceiveDetPojo> queryReceiveDel(QueryReceiveDetReq param) {
List<ReceiveDetPojo> pojos = Collections.synchronizedList(new ArrayList(200000));
List<String> yearMonthParam = getMonthBetween(getYearMonth(param.getReceiveStartDate()), getYearMonth(param.getReceiveEndDate()));
int poolSize = 5;
// 初始化线程池
ExecutorService pool = Executors.newFixedThreadPool(poolSize);
// 初始化计数器
CountDownLatch latch = new CountDownLatch(poolSize);
List<List<String>> codeList = Utils.averageAssign(yearMonthParam, poolSize);
for (List<String> codes : codeList) {
pool.submit(new Runnable() {
@Override
public void run() {
try {
// 将结果汇总
for (int i = 0; i < codes.size(); i++) {
QueryReceiveDetReq queryReceiveDetReq = new QueryReceiveDetReq();
BeanUtils.copyProperties(param, queryReceiveDetReq);
queryReceiveDetReq.setPeriodMonth(codes.get(i));
pojos.addAll(getReceivedDelList(queryReceiveDetReq));
}
} catch (Exception e) {
logger.error("查询数据异常" + e);
} finally {
//线程结束-1
latch.countDown();
}
}
});
}
// 等待所有查询结束
//暂停当前线程,死循环 判断线程数是否结束
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
throw new BusinessException("线程结束异常");
}
if (CollectionUtils.isEmpty(pojos)){
return Collections.EMPTY_LIST;
}
return pojos;
}
public static List<List<String>> averageAssign(List<String> source, int n) {
List<List<String>> result = new ArrayList<>();
int remaider = source.size() % n; //(先计算出余数)
int number = source.size() / n; //然后是商
int offset = 0;//偏移量
for (int i = 0; i < n; i++) {
List<String> value = null;
if (remaider > 0) {
value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
remaider--;
offset++;
} else {
value = source.subList(i * number + offset, (i + 1) * number + offset);
}
result.add(value);
}
return result;
}