时博文 2021-03-29 16:30 采纳率: 100%
浏览 23
已结题

多个线程同步代码块效率问题

    public void saveList(String s) throws ExecutionException, InterruptedException, TimeoutException {
        String orgId = "1111";
        Map<String, Object> params = new HashMap<>();
        params.put("orgId", orgId);
        //如果没有传参  取昨天的0点到24点的时间戳
        if (StringUtils.isEmpty(s)) {
            params.put("startTime", LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli() - 86400000L);
            params.put("endTime", LocalDateTime.of(LocalDate.now(), LocalTime.MAX).toInstant(ZoneOffset.ofHours(8)).toEpochMilli() - 86400000L);
        } else {
            //有值取指定的时间戳
            String[] split = s.split(",");
            params.put("startTime", Long.valueOf(split[0]));
            params.put("endTime", Long.valueOf(split[1]));
        }
        CompletableFuture future2 = CompletableFuture.runAsync(() -> {
            try {
                synchronized (params){
                    params.put("type",2);
                    List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params);
                    if (list != null && list.size() > 0) {
                        rfTxjobImplementDao.saveByList(list, 2);

                    }
                }
//                Map<String, Object> params2 = params;
//                params2.put("type", 2);
//
//                List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params2);
//                if (list != null && list.size() > 0) {
//                    rfTxjobImplementDao.saveByList(list, 2);
//
//                }
            } catch (Exception e) {
                log.error("错误" + e.toString());
            }
        });
        CompletableFuture future3 = CompletableFuture.runAsync(() -> {

            try {
                synchronized (params){
                    params.put("type",3);
                    List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params);
                    if (list != null && list.size() > 0) {
                        rfTxjobImplementDao.saveByList(list, 3);

                    }
                }
//                Map<String, Object> params3 = params;
//                params3.put("type", 3);
//
//                List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params3);
//                if (list!=null &&list.size()>0){
//                    rfTxjobImplementDao.saveByList(list, 3);
//
//                }
            } catch (Exception e) {
                log.error("错误" + e.toString());
            }
        });
        CompletableFuture future4 = CompletableFuture.runAsync(() -> {

            try {
                synchronized (params){
                    params.put("type",4);
                    List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params);
                    if (list != null && list.size() > 0) {
                        rfTxjobImplementDao.saveByList(list, 4);

                    }
                }
//                Map<String, Object> params4 = params;
//                params4.put("type", 4);
//                List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params4);
//                if (list!=null &&list.size()>0){
//                    rfTxjobImplementDao.saveByList(list, 4);
//
//                }
            } catch (Exception e) {
                log.error("错误" + e.toString());
            }
        });

        future2.get(5, TimeUnit.SECONDS);
        future3.get(5, TimeUnit.SECONDS);
        future4.get(5, TimeUnit.SECONDS);


    }

三个异步线程都操作了map,然后整个线程里的逻辑都在同步代码块内,三个线程的效率是不是和写成同步的一样了

  • 写回答

1条回答 默认 最新

  • 三岁丫 2021-03-29 16:55
    关注

    如果是现在写法的话,确实是差不多的

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

报告相同问题?

问题事件

  • 系统已结题 3月31日
  • 已采纳回答 3月23日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装