多线程技术优化:Callable,FutureTask,Task.get()

2021-06-04 12:04

阅读:577

标签:values   als   dir   hash   read   ===   base   tran   wls   

Map brmMap = new HashMap();
        long beginTimeSum = System.currentTimeMillis();
        for(StkInventoryWlTypeEnum emum:StkInventoryWlTypeEnum.values()){
            long beginTime = System.currentTimeMillis();
            Callable aCallable =()->{
                if (OrgNumberEunm.FA_FJ.getOrgNumber().equals(stkInventoryWlDto.getOrg())) {
                    return stkInventoryWlServicemap.get(emum.getCode()).getStkInventoryWlFq(stkInventoryWlDto);

                } else {
                    return stkInventoryWlServicemap.get(emum.getCode()).getStkInventoryWlHn(stkInventoryWlDto);
                }
            };
            FutureTask aCallableTask = new FutureTask(aCallable);
            new Thread(aCallableTask,emum.getName()+"|线程|"+emum.getCode()).start();
            brmMap.put(emum.getName()+"["+emum.getCode()+"]",aCallableTask.get());//花时在等待(解决方案:Map callableTaskGetMap = new HashMap();,主线程走完后再取aCallableTask.get())
long endTime = System.currentTimeMillis(); 
logger.info("花时:"+emum.getName()+"["+emum.getCode()+"]"+((endTime-beginTime))+"ms");
}
long endTimeSum = System.currentTimeMillis();
logger.info("总合计-花时:"+((endTimeSum-beginTimeSum))+"ms");

  

 ==================================================== 
花时:标准其他出库单-铝业[STK_MISDELIVERY]2727s
花时:采购入库单-铝业[STK_INSTOCK]1211s
花时:标准直接调拨单[STK_TRANSFERDIRECT]1224s
花时:即时库存[STK_INVENTORY]709s
总合计-花时:5872S
======================================================
花时:标准其他出库单-铝业[STK_MISDELIVERY]2002ms
花时: 采购入库单-铝业[STK_INSTOCK]886ms
花时:标准直接调拨单[STK_TRANSFERDIRECT]771ms
花时: 即时库存[STK_INVENTORY]715ms
总合计-花时:4378ms 总合计-花时:4279ms
====================================================
加一个callableTaskGetMap用来存数据,FutureTask键值:减少等待时间aCallableTask.get()
 Map callableTaskGetMap = new HashMap();
====================================================
Map brmMap = new HashMap();
        Map callableTaskGetMap = new HashMap();
        long beginTimeSum = System.currentTimeMillis();
        for(StkInventoryWlTypeEnum emum:StkInventoryWlTypeEnum.values()){
            long beginTime = System.currentTimeMillis();
            Callable aCallable =()->{
                if (OrgNumberEunm.FA_FJ.getOrgNumber().equals(stkInventoryWlDto.getOrg())) {
                    return stkInventoryWlServicemap.get(emum.getCode()).getStkInventoryWlFq(stkInventoryWlDto);

                } else {
                    return stkInventoryWlServicemap.get(emum.getCode()).getStkInventoryWlHn(stkInventoryWlDto);
                }
            };
            FutureTask aCallableTask = new FutureTask(aCallable);
            new Thread(aCallableTask,emum.getName()+"|线程|"+emum.getCode()).start();
            callableTaskGetMap.put(emum.getCode(),aCallableTask);//
            long endTime = System.currentTimeMillis();
            logger.info("花时:"+emum.getName()+"["+emum.getCode()+"]"+((endTime-beginTime))+"ms");
        }

        brmMap.put(StkInventoryWlTypeEnum.STK_MISDELIVERY.getName()+"["+StkInventoryWlTypeEnum.STK_MISDELIVERY.getCode()+"]",callableTaskGetMap.get(StkInventoryWlTypeEnum.STK_MISDELIVERY.getCode()).get());
        brmMap.put(StkInventoryWlTypeEnum.STK_INSTOCK.getName()+"["+StkInventoryWlTypeEnum.STK_INSTOCK.getCode()+"]",callableTaskGetMap.get(StkInventoryWlTypeEnum.STK_INSTOCK.getCode()).get());
        brmMap.put(StkInventoryWlTypeEnum.STK_TRANSFERDIRECT.getName()+"["+StkInventoryWlTypeEnum.STK_TRANSFERDIRECT.getCode()+"]",callableTaskGetMap.get(StkInventoryWlTypeEnum.STK_TRANSFERDIRECT.getCode()).get());
        brmMap.put(StkInventoryWlTypeEnum.STK_INVENTORY.getName()+"["+StkInventoryWlTypeEnum.STK_INVENTORY.getCode()+"]",callableTaskGetMap.get(StkInventoryWlTypeEnum.STK_INVENTORY.getCode()).get());
        long endTimeSum = System.currentTimeMillis();
        logger.info("总合计-花时:"+((endTimeSum-beginTimeSum))+"ms");

  

======================================================
多线程技术优化后数据:花时:大约时间少了效果提升一倍, 总合计-花时:2427ms
======================================================

多线程技术优化:Callable,FutureTask,Task.get()

标签:values   als   dir   hash   read   ===   base   tran   wls   

原文地址:https://www.cnblogs.com/gzhbk/p/14652309.html


评论


亲,登录后才可以留言!