2

I am studying computer science at a University, and I have been assigned to create a 'normal' program, a program using implicit concurrency (parallel streams) and a program using explicit concurrency (threadpool)

I have created a batch file copier (source to destination) using the Java.NIO lib. I am trying to figure out how it would be possible to be done using parallel streams.

I have a String array which contains the file paths/names, and then I have this loop in order to copy the files to the destination:

int i = 0;
while (i < filelist.length){
    String filepath = filelist[i];
    Path source = Paths.get(filepath);
    Path target = Paths.get(FileBrowser.destinationpath+"/"+filenames[i]);  

    try {
        //replace existing file using java nio package
        System.out.println(Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING));
    } catch (IOException e) {
        e.printStackTrace();
    }
    i++;
}

Any help on how to do this by using parallel streams would be appreciated.

PS. I know that copying files in parallel would not result in the files being copied faster rather than sequential, since it is not a CPU matter and more of a disk matter. Either way this assignment is to test and report results so that's why I have chosen this approach.

2
  • it could be faster if you have more than one disk. Also note that you would probably get better results with a parallel stream on Java 9 vs 8 because the performance has been improved: bugs.openjdk.java.net/browse/JDK-8072773 Commented Mar 18, 2016 at 17:07
  • @assylias thank you for the heads up, is what I am asking possible though? I can't seem to figure out how to apply that chunk of code as parallel stream. Commented Mar 18, 2016 at 18:02

1 Answer 1

1

Using Java 8, you can iterate over any list as a parallel stream. So using your code, you could do something as the following:

Arrays.asList(filelist).parallelStream().forEach((filepath) -> {
    Path source = Paths.get(filepath);
    Path target = Paths.get(FileBrowser.destinationpath+"/"+filepath]);
    try {
        //replace existing file using java nio package
        System.out.println(Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING));
    } catch (IOException e) {
        e.printStackTrace();
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your response, I can't use this: Path target = Paths.get(FileBrowser.destinationpath+"/"+filepath); I have a different string array for the filename, Path target = Paths.get(FileBrowser.destinationpath+"/"+filenames[i]);
I have modified your answer and it works :) I get better times copying files through this parallel stream too! Files: 85 Total Size: 440.57 MB NORMAL 38.79 35.98 36.24 PARALLEL 32.93 32.63 35.19 THREADPOOL 37.53 40.06 36.01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.