Skip to content

Instantly share code, notes, and snippets.

@coderplay
Last active January 31, 2016 16:27
Show Gist options
  • Save coderplay/5864724 to your computer and use it in GitHub Desktop.
Save coderplay/5864724 to your computer and use it in GitHub Desktop.
Counting the size of a directory
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.io.IOException;
public class JavaDU {
public static class CountingTask extends RecursiveTask<Long> {
private Path dir;
public CountingTask(Path dir) {
this.dir = dir;
}
@Override
protected Long compute() {
long size = 0;
List<CountingTask> subTasks = new ArrayList<CountingTask>();
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) {
for (Path subPath : ds) {
if (Files.isDirectory(subPath, LinkOption.NOFOLLOW_LINKS)) {
subTasks.add(new CountingTask(subPath));
} else {
long s = Files.size(subPath);
// System.out.println(s + "\t" + subPath);
size += s;
}
}
if (!subTasks.isEmpty()) {
for (CountingTask subTask : invokeAll(subTasks)) {
long s = subTask.join();
// System.out.println(s + "\t" + subTask.dir);
size += s;
}
}
} catch (IOException ex) {
// ex.printStackTrace();
return 0L;
}
return size;
}
}
public static void main(String[] args) {
int parallelism = Runtime.getRuntime().availableProcessors() << 1;
Long size = new ForkJoinPool(parallelism).invoke(
new CountingTask(Paths.get(args[0])));
System.out.println(size + "\t" + args[0]);
System.out.println(size + "\ttotal");
}
}
@hugozhu
Copy link

hugozhu commented Jun 26, 2013

不错,fork/join解决了划分出来的任务在线程间均匀分配的问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment