有个问题想请教下各位大佬,重构前程序(批量创建文件)运行需要23秒,使用Fork/Join重构后的只要4秒钟,设置了同样的阈值,部署到生产环境中,还是需要23秒,生产上面使用的是阿里云的ECS 有4个vCPU,线程池使用的ForkJoinPool,按道理应该跟本地效果一样才对。这是什么原因导致的呢?
public void createLoadContract(){
commonResult = new CommonResult();
commonResult.setCodeAndMsg(BaseConst.SUCCESS_CODE,BaseConst.SUCCESS_MSG);
String fenqiLineIds = getPara("fenqiLineIds");
Integer contractProperty = getParaToInt("contractProperty");
String[] fenqiLineIdsStr = fenqiLineIds.split(",");
Long insureInfoId = getParaToLong("insureInfoId");
ForkJoinPool pool = new ForkJoinPool();
ContractFileTask innerFind = new ContractFileTask(fenqiLineIdsStr,0,fenqiLineIdsStr.length-1,contractProperty,insureInfoId);
long start = System.currentTimeMillis();
//同步调用
pool.invoke(innerFind);
int sucessCount= innerFind.join();
if(fenqiLineIdsStr.length==sucessCount){
//创建合同成功
renderJson(commonResult);
}else{
//创建合同失败
commonResult.setCodeAndMsg(BaseConst.ERROR_CODE,"创建合同失败!");
renderJson(commonResult);
}
}
private class ContractFileTask extends RecursiveTask<Integer> {
/**
* 设置阈值
*/
private final static int THRESHOLD = 4;
/**
* 表示我们要实际统计的数组
*/
private String[] fenqiLineIdsStr;
/**
* 开始统计的下标
*/
private int fromIndex;
/**
* 统计到哪里结束的下标
*/
private int toIndex;
/**
*合同类型
*/
private Integer contractProperty;
/**
*用户Id
*/
private Long insureInfoId;
public ContractFileTask(String[] fenqiLineIdsStr, int fromIndex, int toIndex,Integer contractProperty,Long insureInfoId) {
this.fenqiLineIdsStr = fenqiLineIdsStr;
this.fromIndex = fromIndex;
this.toIndex = toIndex;
this.contractProperty=contractProperty;
this.insureInfoId=insureInfoId;
}
@Override
protected Integer compute() {
//如果小于阈值的话 直接执行
if(toIndex-fromIndex < THRESHOLD) {
//int count = 0;
String[] fb = Arrays.copyOfRange(fenqiLineIdsStr, fromIndex, toIndex+1);
return createLoadContractProcess(fb,contractProperty,insureInfoId);
}else {
//如果大于阈值的话,继续拆分
int mid = (fromIndex+toIndex)/2;
ContractFileTask left = new ContractFileTask(fenqiLineIdsStr,fromIndex,mid,contractProperty,insureInfoId);
ContractFileTask right = new ContractFileTask(fenqiLineIdsStr,mid+1,toIndex,contractProperty,insureInfoId);
invokeAll(left,right);
return left.join()+right.join();
}
}
}