private void checkIsUploadAndUpdateFile() { try { ExecutorService executorService = Executors.newFixedThreadPool(2); //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) { List 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]);//文件时间版本

                        if (!ids.contains(key)) {
                            //把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) {
                                    e.printStackTrace();
                                }

                            } catch (IOException | ClassNotFoundException e) {
                                throw new RuntimeException(e);
                            }
                        } else 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) {
                                        e.printStackTrace();
                                    }
                                    reName(mapSycTrainPath + file.getName(), mapSycTrainPath + key + "-" + value.time + ".json");//修改文件名
                                } catch (IOException | ClassNotFoundException e) {
                                    throw new RuntimeException(e);
                                }
                            });
                            executorService.submit(fileOperationThread);
                        }
                    }
                }
            });
            executorService.submit(fileCheckThread);
        }
    }
    executorService.shutdown();
} catch (Exception e) {
    e.printStackTrace();
}
private void checkIsUploadAndUpdateFile try ExecutorService executorService = ExecutorsnewFixedThreadPool2; 1循环训练场allMapSycTrainMap中数据-加锁 synchronized allMapS

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

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