侧边栏壁纸
博主头像
小小酥心

旧书不厌百回读,熟读精思子自知💪

  • 累计撰写 22 篇文章
  • 累计创建 8 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

使用forkJoin高效读取文件夹大小

小小酥心
2022-01-12 / 0 评论 / 0 点赞 / 1,070 阅读 / 1,454 字
温馨提示:
本文最后更新于 2022-01-12,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

forkJoin采用分支合并思想,将一个大的任务拆分成多个子任务进行并行处理,最后将子任务结果合并成最后的计算结果。代码如下:

private static class FileSizeFinder extends RecursiveTask<Long> {
    final File file;

    public FileSizeFinder(final File theFile) {
        file = theFile;
    }

    @Override
    public Long compute() {
        long size = 0;
        if (file.isFile()) {
            size = file.length();
        } else {
            final File[] children = file.listFiles();
            if (children != null) {
                List<ForkJoinTask<Long>> tasks = new ArrayList<>();
                for (final File child : children) {
                    if (child.isFile()) {
                        size += child.length();
                    } else {
                        tasks.add(new FileSizeFinder(child));
                    }
                }
                for (final ForkJoinTask<Long> task : invokeAll(tasks)) {
                    size += task.join();
                }
            }
        }
        return size;
    }
}

单线程递归方式:

public static long getTotalSizeOfFilesInDir(final File file) {
    if (file.isFile()) {
        return file.length();
    }
    final File[] children = file.listFiles();
    long total = 0;
    if (children != null) {
        for (final File child : children) {
            total += getTotalSizeOfFilesInDir(child);
        }
    }
    return total;
}

比较两者性能

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ForkJoinPool pool = new ForkJoinPool();
    long start = System.nanoTime();
    ForkJoinTask<Long> forkJoinTask = pool.submit(new FileSizeFinder(new File("D:\\Game")));
    long end = System.nanoTime();
    System.out.println(forkJoinTask.get());
    System.out.println("耗时:" + (end - start));
    
    start = System.nanoTime();
    long size = getTotalSizeOfFilesInDir(new File("D:\\Game"));
    end = System.nanoTime();
    System.out.println(size);
    System.out.println("耗时:" + (end - start));
}

测试结果:

430850200261
耗时:1014900
430850200261
耗时:1776610500
0

评论区