Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1519187
  • 博文数量: 399
  • 博客积分: 8508
  • 博客等级: 中将
  • 技术积分: 5302
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-14 09:28
个人简介

能力强的人善于解决问题,有智慧的人善于绕过问题。 区别很微妙,小心谨慎做后者。

文章分类

全部博文(399)

文章存档

2018年(3)

2017年(1)

2016年(1)

2015年(69)

2013年(14)

2012年(17)

2011年(12)

2010年(189)

2009年(93)

分类: 架构设计与优化

2015-05-01 20:10:48

public class FileSize {
    private final static ForkJoinPool forkJoinPool = new ForkJoinPool();
    private static class FileSizeFinder extends RecursiveTask { 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> tasks = new ArrayList>();

            for(final File child : children) {

                if (child.isFile()) {
                    size += child.length();
                } else {
                    tasks.add(new FileSizeFinder(child));
                }
            }
            for(final ForkJoinTask task : invokeAll(tasks)) {
             size += task.join();}
            }
        }
        return size; }
    }
    public static void main(final String[] args) {
        final long start = System.nanoTime();
        final long total = forkJoinPool.invoke(new FileSizeFinder(new File(args[0])));
        final long end = System.nanoTime();

        System.out.println("Total Size: " + total);
        System.out.println("Time taken: " + (end - start)/1.0e9);
    }
}
阅读(848) | 评论(0) | 转发(0) |
0

上一篇:Java 高并发有效 策略

下一篇:转账

给主人留下些什么吧!~~