diff --git a/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java b/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java index 317b8216eab6917cc29e0a022dc0a76663781f83..0bf92dc1c13039bca22f4db299930d29a14e2c02 100644 --- a/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java +++ b/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; /** * 删除博客中不存在的照片 @@ -25,7 +26,7 @@ public class FileContainsUtil { /** * 默认图片不存在 */ - private static boolean IS_EXIST = false; + private static ThreadLocal IS_EXIST = ThreadLocal.withInitial(() -> false); /** * 图片路径 */ @@ -61,32 +62,8 @@ public class FileContainsUtil { final List picNames = getPicName(PIC_PATH); final int size = picNames.size(); log.info("图片总数为{}", size); - if (CollectionUtil.isNotEmpty(picNames)) { - outerloop: - for (int i = 0; i < size; i++) { - String picName = picNames.get(i); - //是白名单里面的图片,直接忽略 - for (String picPathWhitelist : PIC_PATH_WHITELISTS) { - if (picPathWhitelist.contains(picName)) { - continue outerloop; - } - } - //默认不存在 - IS_EXIST = false; - //指定类型的文件 - List suffix = new ArrayList<>(); - suffix.add(".md"); - //包含某个字符串 - traverseFolder(BLOG_FOLDER1, suffix, picName); - traverseFolder(BLOG_FOLDER2, suffix, picName); - traverseFolder(BLOG_FOLDER3, suffix, picName); - //文件不存在 - if (!IS_EXIST) { - isNotExist.add(picName); - deletePic(PIC_PATH + picName); - } - } - } + List> groupedPicNames = dataGroup(picNames); + moreThread(groupedPicNames, isNotExist); log.info("不存在图片总数为{}", isNotExist.size()); stopWatch.stop(); //毫秒输出 @@ -95,15 +72,85 @@ public class FileContainsUtil { log.info("耗时分钟数:{}", JSON.toJSONString(stopWatch.getTotalTimeSeconds() / 60)); } + /** + * 多线程处理图片任务 + * + * @param groupedPicNames + * @param isNotExist + */ + private static void moreThread(List> groupedPicNames, List isNotExist) { + // 创建并启动4个线程来处理每个组的任务 + List threads = new ArrayList<>(); + for (int i = 0; i < 6; i++) { + final List group = groupedPicNames.get(i); + Thread thread = new Thread(() -> { + // 在这里执行处理图片组的任务 + if (CollectionUtil.isNotEmpty(group)) { + for (String picName : group) { + // 处理图片文件 picName + //是白名单里面的图片,直接忽略 + for (String picPathWhitelist : PIC_PATH_WHITELISTS) { + if (picPathWhitelist.contains(picName)) { + continue; + } + } + //默认不存在 + IS_EXIST.set(false); + //包含某个字符串 + try { + traverseFolder(BLOG_FOLDER1, picName); + traverseFolder(BLOG_FOLDER2, picName); + traverseFolder(BLOG_FOLDER3, picName); + } catch (Exception e) { + e.printStackTrace(); + } + //文件不存在 + if (!IS_EXIST.get()) { + isNotExist.add(picName); + deletePic(PIC_PATH + picName); + } + } + } + + }); + thread.start(); + threads.add(thread); + } + // 等待所有线程完成 + for (Thread thread : threads) { + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + /** + * 数据分组 + * + * @param picNames + * @return + */ + private static List> dataGroup(List picNames) { + int groupSize = picNames.size() / 6; // 计算每组的大小 + List> groupedPicNames = + picNames.stream() + .collect(Collectors.groupingBy(e -> picNames.indexOf(e) / groupSize)) + .values() + .stream() + .collect(Collectors.toList()); + return groupedPicNames; + } + /** * 获取文件 * * @param path - * @param suffixs * @param word * @throws IOException */ - public static void traverseFolder(String path, List suffixs, String word) throws Exception { + public static void traverseFolder(String path, String word) throws Exception { File file = new File(path); if (file.exists()) { //获取文件夹下的文件 @@ -112,13 +159,11 @@ public class FileContainsUtil { for (File file2 : files) { //是否是文件夹 if (file2.isDirectory()) { - traverseFolder(file2.getAbsolutePath(), suffixs, word); + traverseFolder(file2.getAbsolutePath(), word); } else { - for (String suffix : suffixs) { - //包含md结尾的文件 - if (file2.getAbsolutePath().contains(suffix)) { - getParams(file2.getAbsolutePath(), word); - } + //包含md结尾的文件 + if (file2.getAbsolutePath().contains(".md")) { + getParams(file2.getAbsolutePath(), word); } } } @@ -141,7 +186,7 @@ public class FileContainsUtil { //判断是否包含方法名称,即指定字符串 if (string.contains(word)) { //文件存在 - IS_EXIST = true; + IS_EXIST.set(true); } } }