比较Fork和Join与单线程程序

时间:2015-03-12 06:03:40

标签: java multithreading fork-join

我正在尝试使用Fork-Join框架来完成较小的任务。在我启动的例子中,我尝试复制mp3文件

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class DeepFileCopier extends RecursiveTask<String>{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private static Path startingDir = Paths.get("D:\\larsen\\Music\\");
    private static List<Path> listOfPaths = new ArrayList<>();
    private int start, end;

    public static void main(String[] args) throws IOException
    {
        long startMillis = System.currentTimeMillis();
        Files.walkFileTree(startingDir, new CustomFileVisitor());
        final DeepFileCopier deepFileCopier = new DeepFileCopier(0,listOfPaths.size());
        final ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
        pool.invoke(deepFileCopier);
        System.out.println("With Fork-Join " + (System.currentTimeMillis() - startMillis));
        long secondStartMillis = System.currentTimeMillis();
        deepFileCopier.start = 0;
        deepFileCopier.end = listOfPaths.size();
        deepFileCopier.computeDirectly();
        System.out.println("Without Fork-Join " + (System.currentTimeMillis() - secondStartMillis));

    }

    private static class CustomFileVisitor extends SimpleFileVisitor<Path> {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
        {
            if (file.toString().endsWith(".mp3")) {
                listOfPaths.add(file);
            }
            return FileVisitResult.CONTINUE;
        }
    }

    @Override
    protected String compute() {
        int length = end-start;
        if(length < 4) {
            return computeDirectly();
        }
        int split = length / 2;
        final DeepFileCopier firstHalfCopier = new DeepFileCopier(start, start + split);
        firstHalfCopier.fork();
        final DeepFileCopier secondHalfCopier = new DeepFileCopier(start + split, end);
        secondHalfCopier.compute();
        firstHalfCopier.join();
        return null;
    }

    private String computeDirectly() {
        for(int index = start; index< end; index++) {
            Path currentFile = listOfPaths.get(index);
            System.out.println("Copying :: " + currentFile.getFileName()); 
            Path targetDir = Paths.get("D:\\Fork-Join Test\\" + currentFile.getFileName());
            try {
                Files.copy(currentFile, targetDir, StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private DeepFileCopier(int start, int end ) {
        this.start = start;
        this.end = end;
    }

}

比较我注意到的表现 -

使用Fork-Join 149714 没有Fork-Join 146590

正在使用双核机器。我期待工作时间缩短50%,但使用Fork-Join的部分比单线程方法多3秒。如果有些事情不正确,请告诉我。

1 个答案:

答案 0 :(得分:2)

您的问题不适合在普通系统上受益于多线程。执行时间用于复制所有文件。但这受限于您按顺序处理文件的硬盘驱动器。

如果你运行更加CPU密集的任务,你应该注意到一个区别。出于测试目的,您可以尝试以下方法:

private String computeDirectly() {
   Integer nonsense;
   for(int index = start; index< end; index++) {
      for( int j = 0; j < 1000000; j++ )
         nonsense += index*j;
    }
    return nonsense.toString();
}

在我的系统(i5-2410M)上将打印:

With Fork-Join 2628
Without Fork-Join 6421