优化代码中抛异常的代码可以将异常抛出给调用方处理,而不是在方法内部直接打印堆栈轨迹,如下所示:

private void checkIsUploadAndUpdateFileTrain() throws IOException, ClassNotFoundException {
    try {
        //1.循环训练场allMapSycTrainMap中数据-加锁
        synchronized (allMapSycTrainMap) {
            for (Map.Entry<Long, MapSycTrain> entry : allMapSycTrainMap.entrySet()) {
                Long key = entry.getKey();
                MapSycTrain value = entry.getValue();
                Thread fileCheckThread = new Thread(() -> {
                    File folder = new File(mapSycTrainPath); // 本地文件夹路径
                    File[] files = folder.listFiles();
                    if (files != null) {
                        //存储文件里所有id集合
                        List<Long> ids = new ArrayList<>();
                        for (File file : files) {
                            String[] fileName1 = file.getName().split("-");
                            Long trainingGroundIdByLocal = Long.valueOf(fileName1[0]);//文件训练场ID
                            ids.add(trainingGroundIdByLocal);
                        }

                        for (File file : files) {
                            String[] fileName1 = file.getName().split("-");
                            String[] fileName2 = fileName1[1].split("\\.");
                            Long trainingGroundIdByLocal = Long.valueOf(fileName1[0]);//文件训练场ID
                            Long timeByLocal = Long.valueOf(fileName2[0]);//文件时间版本

                            //文件中所有的训练场ID没有找到map中的训练场ID时,创建新的文件
                            if (!ids.contains(key)) {
                                //开启新线程去处理
                                Thread fileOperationThread = new Thread(() -> {
                                    //把Detail数据深拷贝一份,(使用io线程写入文件)将内存中的数据(深拷贝的数据)更新到本地json文件
                                    List<DetailRouteMapData> detailRouteMapDataList = getDetailRouteMapDataListByTrainingGroundIdOrProjectRouteMapDataId(key);
                                    try {
                                        List<DetailRouteMapData> copiedList = deepCopy(detailRouteMapDataList);//深拷贝
                                        Map<Long, DetailRouteMapData> map = new HashMap<>();
                                        for (DetailRouteMapData data : copiedList) {
                                            map.put(data.getId(), data);
                                        }
                                        String json = JSON.toJSONString(map);
                                        String filePath = mapSycTrainPath + key + "-" + System.currentTimeMillis() + ".json";
                                        try (FileWriter fileWriter = new FileWriter(filePath)) {
                                            // 将JSON对象写入文件
                                            fileWriter.write(json);
                                            fileWriter.flush();
                                        } catch (IOException e) {
                                            throw new RuntimeException(e);
                                        }
                                    } catch (IOException | ClassNotFoundException e) {
                                        throw new RuntimeException(e);
                                    }
                                });
                                fileOperationThread.start();
                            }

                            //当训练场ID相同时,时间版本不同
                            if (Objects.equals(key, trainingGroundIdByLocal) && !Objects.equals(value.time, timeByLocal)) {
                                // 开启新的线程去操作
                                Thread fileOperationThread = new Thread(() -> {
                                    //把Detail数据深拷贝一份,(使用io线程写入文件)将内存中的数据(深拷贝的数据)更新到本地json文件
                                    List<DetailRouteMapData> detailRouteMapDataList = getDetailRouteMapDataListByTrainingGroundIdOrProjectRouteMapDataId(key);
                                    try {
                                        List<DetailRouteMapData> copiedList = deepCopy(detailRouteMapDataList);//深拷贝
                                        Map<Long, DetailRouteMapData> map = new HashMap<>();
                                        for (DetailRouteMapData data : copiedList) {
                                            map.put(data.getId(), data);
                                        }
                                        String json = JSON.toJSONString(map);
                                        String filePath = mapSycTrainPath + file.getName();
                                        // 使用 try-with-resources 语句确保文件资源正确关闭
                                        try (FileOutputStream fos = new FileOutputStream(filePath, false);
                                             OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
                                             BufferedWriter writer = new BufferedWriter(osw)) {
                                            // 清空文件内容
                                            writer.write("");
                                            // 写入JSON字符串到文件
                                            writer.write(json);
                                        } catch (IOException e) {
                                            throw new RuntimeException(e);
                                        }
                                        reName(mapSycTrainPath + file.getName(), mapSycTrainPath + key + "-" + value.time + ".json");//修改文件名
                                    } catch (IOException | ClassNotFoundException e) {
                                        throw new RuntimeException(e);
                                    }
                                });
                                fileOperationThread.start();
                            }
                        }
                    }
                });
                fileCheckThread.start();
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

在方法签名中声明了throws IOException, ClassNotFoundException,表示该方法可能会抛出这两种异常,调用方可以根据需要进行处理

private void checkIsUploadAndUpdateFileTrain try 1循环训练场allMapSycTrainMap中数据-加锁 synchronized allMapSycTrainMap for MapEntryLong MapSycTrain entry allMapS

原文地址: https://www.cveoy.top/t/topic/iNJe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录