Java 文件监控与更新:使用多线程处理两个 Map 数据
private void checkIsUploadAndUpdateFile() {
try {
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(() -> {
synchronized (allMapSycTrainMap) {
//1.循环allMapSycTrainMap中数据-加锁
for (Map.Entry<Long, MapSycTrain> entry : allMapSycTrainMap.entrySet()) {
Long key = entry.getKey();
MapSycTrain value = entry.getValue();
Thread fileCheckThread = new Thread(() -> {
//持续监测指定文件夹中是否有新的文件出现
while (true) {
File folder = new File('D:/mapSycTrain/'); // 本地文件夹路径
File[] files = folder.listFiles();
if (files != null) {
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相同时,时间版本不同
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);
String json = JSON.toJSONString(copiedList);
String filePath = 'D:/mapSycTrain/' + file.getName();
new Thread(new Runnable() {
@Override
public void run() {
try {
// 清空文件内容
FileWriter fileWriter = new FileWriter(filePath, false);
fileWriter.write("");
fileWriter.close();
// 写入JSON字符串到文件
FileWriter fileWriterAppend = new FileWriter(filePath, true);
fileWriterAppend.write(json);
fileWriterAppend.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
});
fileOperationThread.start();
}
}
}
}
});
executorService.submit(fileCheckThread);
}
}
});
executorService.submit(() -> {
synchronized (allMapSycProjectMap) {
//1.循环allMapSycTrainMap中数据-加锁
for (Map.Entry<Long, MapSycProject> entry : allMapSycProjectMap.entrySet()) {
Long key = entry.getKey();
MapSycProject value = entry.getValue();
Thread fileCheckThread = new Thread(() -> {
//持续监测指定文件夹中是否有新的文件出现
while (true) {
File folder = new File('D:/mapSycProject/'); // 本地文件夹路径
File[] files = folder.listFiles();
if (files != null) {
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相同时,时间版本不同
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);
String json = JSON.toJSONString(copiedList);
String filePath = 'D:/mapSycProject/' + file.getName();
new Thread(new Runnable() {
@Override
public void run() {
try {
// 清空文件内容
FileWriter fileWriter = new FileWriter(filePath, false);
fileWriter.write("");
fileWriter.close();
// 写入JSON字符串到文件
FileWriter fileWriterAppend = new FileWriter(filePath, true);
fileWriterAppend.write(json);
fileWriterAppend.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
});
fileOperationThread.start();
}
}
}
}
});
executorService.submit(fileCheckThread);
}
}
});
executorService.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
This modified code uses two ExecutorService threads to handle the loop operations on the two different maps. Each thread is responsible for checking and updating files in a specific folder. The ExecutorService manages the thread pool and ensures the simultaneous execution of these threads. This approach allows the operations on the two maps to run concurrently without interfering with each other.
原文地址: https://www.cveoy.top/t/topic/qtca 著作权归作者所有。请勿转载和采集!