码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2021-04-13 12:56:07      阅读:0      评论:0      收藏:0      [点我收藏+]

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

Map<String,BaseResultMessage> brmMap = new HashMap<>();
        long beginTimeSum = System.currentTimeMillis();
        for(StkInventoryWlTypeEnum emum:StkInventoryWlTypeEnum.values()){
            long beginTime = System.currentTimeMillis();
            Callable<BaseResultMessage> 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<BaseResultMessage> aCallableTask = new FutureTask(aCallable);
            new Thread(aCallableTask,emum.getName()+"|线程|"+emum.getCode()).start();
            brmMap.put(emum.getName()+"["+emum.getCode()+"]",aCallableTask.get());//花时在等待(解决方案:Map<String,FutureTask> 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<String,FutureTask> callableTaskGetMap = new HashMap<>();
====================================================
Map<String, Object> brmMap = new HashMap<>();
        Map<String,FutureTask> callableTaskGetMap = new HashMap<>();
        long beginTimeSum = System.currentTimeMillis();
        for(StkInventoryWlTypeEnum emum:StkInventoryWlTypeEnum.values()){
            long beginTime = System.currentTimeMillis();
            Callable<BaseResultMessage> 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<BaseResultMessage> 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!