「已注销」 2018-07-31 02:11 采纳率: 100%
浏览 497
已采纳

使用Fork/Join重构代码后,生产环境和重构前效果一样

有个问题想请教下各位大佬,重构前程序(批量创建文件)运行需要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();
            }
        }
    }
  • 写回答

1条回答 默认 最新

  • COCO_AS 2018-08-02 14:07
    关注

    这样比较没有多大意义, 最好试一下重构前在阿里云上的时间. 因为不同机器, 参数也可能不一样(内存, 磁盘IO, JDK版本等等).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!